ntree.ai
ntree.aiWord Break

Word Break

mediumsolvedattempted

Given a string s and a list of words wordDict, return true if s can be split into a sequence of one or more words from wordDict.

The same word can be used any number of times.

Example 1:

Input:  s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: "leetcode" is "leet" followed by "code".

Example 2:

Input:  s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: "applepenapple" is "apple", "pen" and "apple" again.

Example 3:

Input:  s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false
Explanation: "cats" and "cat" both fit at the start, but neither leaves a rest that splits into words.

Constraints:

  • 1 <= length of s <= 300
  • 1 <= length of wordDict <= 1000
  • 1 <= length of wordDict[i] <= 20
  • s and every word hold lowercase English letters only, and the words are all different
def wordBreak(s: str, wordDict: list[str]) -> bool:
pass
 

Press Visualise to step through your code.