Word Break

Given a string s and a dictionary wordDict, return true if s can be segmented into a space-separated sequence of one or more dictionary words. This is a classic state-transition DP problem on prefixes.

Input Format

s = string, wordDict = list of words

Output Format

true if s can be segmented into dictionary words

Constraints

  • 1 <= s.length <= 300; 1 <= wordDict.length <= 1000

Examples

Example 1:

Input:

s = "leetcode"
wordDict = ["leet","code"]

Output:

true

Explanation:

leetcode = leet + code.

Example 2:

Input:

s = "applepenapple"
wordDict = ["apple","pen"]

Output:

true

Explanation:

apple + pen + apple is valid.

Loading...
Word Break - Dp Fundamentals DSA Problem