219. Word Break

MediumStringString

Given a string s and a dictionary of words, return true if s can be segmented into a space-separated sequence of one or more dictionary words (words may be reused). Input: JSON {s, words}.

Input: JSON {s, words}.

Output: Boolean — true or false.

Examples

Example 1
Input: {"s":"leetcode","words":["leet","code"]}
Output: true
Explanation: 'leet'+'code'.
Example 2
Input: {"s":"applepenapple","words":["apple","pen"]}
Output: true
Explanation: Reuse 'apple'.
Example 3
Input: {"s":"catsandog","words":["cats","dog","sand","and","cat"]}
Output: false
Explanation: No full segmentation.

Constraints

Asked by

AmazonAppleBloombergMetaMicrosoftGoogle
Solve this problem in the editor →