📘
Word Break
MediumDynamic Programming
Given a string s and a word dictionary, return True if s can be segmented into space-separated sequence of dictionary words.
Example:
Input:
leetcode leet,code
Output:
True
Reasoning:
- The input string
sis "leetcode" and the word dictionary contains "leet" and "code". - We attempt to segment
sinto a sequence of dictionary words, starting with "leet" which matches the first 4 characters ofs. - The remaining characters "code" also match a word in the dictionary.
- Since the entire string
scan be segmented into a sequence of dictionary words ("leet" and "code"), the output isTrue.
Constraints:
- 1 <= len(s) <= 300
- 1 <= len(wordDict) <= 1000
- 1 <= len(wordDict[i]) <= 20
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.