Medium

Quiz

#139 Word Break

APPROACH

Given a string s and a dictionary wordDict, determine whether s can be segmented into a space-separated sequence of words all drawn from wordDict. Return true if possible.

Words from the dictionary may be reused any number of times.

Example 1:

Input: s = "leetcode", wordDict = ["leet","code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".

Example 2:

Input: s = "applepenapple", wordDict = ["apple","pen"]
Output: true
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
Note that you are allowed to reuse a dictionary word.

Example 3:

Input: s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
Output: false
1 of 4
1:00

What is the optimal approach for this problem?