LeetCode in Kotlin

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

import java.util.HashSet

class Solution {
    fun wordBreak(s: String, wordDict: List<String>): Boolean {
        val set: MutableSet<String> = HashSet()
        var max = 0
        val flag = BooleanArray(s.length + 1)
        for (st in wordDict) {
            set.add(st)
            if (max < st.length) {
                max = st.length
            }
        }
        for (i in 1..max) {
            if (dfs(s, 0, i, max, set, flag)) {
                return true
            }
        }
        return false
    }

    private fun dfs(s: String, start: Int, end: Int, max: Int, set: Set<String>, flag: BooleanArray): Boolean {
        if (!flag[end] && set.contains(s.substring(start, end))) {
            flag[end] = true
            if (end == s.length) {
                return true
            }
            for (i in 1..max) {
                if (end + i <= s.length && dfs(s, end, end + i, max, set, flag)) {
                    return true
                }
            }
        }
        return false
    }
}