LeetCode-in-All

139. Word Break

Medium

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

Note that the same word in the dictionary may be reused multiple times in the segmentation.

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

Constraints:

Solution

object Solution {
    def wordBreak(s: String, wordDict: List[String]): Boolean = {
        val dp = (0 until s.length() + 1).map(_ => false).toBuffer
        dp(0) = true
        val wordSet = wordDict.toSet

        def loop(j: Int, end: Int): Int = {
            if (j < end && dp(j) && wordSet.contains(s.substring(j, end))) {
                dp(end) = true
                end
            }
            else if (j < end) loop(j + 1, end)
            else -1
        }

        for (i <- 1 until s.length() + 1) {
            loop(0, i)
        }
        dp.last
    }
}