v9.1.1
Menu

Word Break

mediumFREEDynamic ProgrammingBlind 75

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 s is "leetcode" and the word dictionary contains "leet" and "code".
  • We attempt to segment s into a sequence of dictionary words, starting with "leet" which matches the first 4 characters of s.
  • The remaining characters "code" also match a word in the dictionary.
  • Since the entire string s can be segmented into a sequence of dictionary words ("leet" and "code"), the output is True.

Constraints

  • 1 <= len(s) <= 300
  • 1 <= len(wordDict) <= 1000
  • 1 <= len(wordDict[i]) <= 20

Learn this in

More from Blind 75

solution.py

Test results

Sign in to run your code against the test cases.