Given a string s
and a dictionary of strings wordDict
, determine if s
can be segmented into a space-separated sequence of dictionary words.
Rules:
Constraints:
Examples:
// Example 1: console.log(wordBreak("neetcode", ["neet","code"])); // Output: true // Explanation: "neetcode" = "neet" + "code" // Example 2: console.log(wordBreak("applepenapple", ["apple","pen"])); // Output: true // Explanation: "applepenapple" = "apple" + "pen" + "apple" // Example 3: console.log(wordBreak("catsincars", ["cats","cat","sin","in","car"])); // Output: false // Explanation: Cannot be segmented with given words