Word Break

Medium

Given a string s and a dictionary wordDict, return true if s can be segmented into a space-separated sequence of dictionary words.

Words may be reused any number of times.

Example 1

wordBreak()
Input
s = "leetcode", wordDict = ["leet","code"]
Output
true

Example 2

wordBreak()
Input
s = "applepenapple", wordDict = ["apple","pen"]
Output
true
Constraints
1 <= s.length <= 300
1 <= wordDict.length <= 1000
Words are lowercase and unique.

Topics

Dynamic ProgrammingStringsTrie

Implement wordBreak in the editor and return the answer — printing it is never graded.