[leetcode 算法练习] - 139.单词拆分

150 阅读1分钟

题目

leetcode 题目地址

参考题解

算法学习 github地址

代码

深度优先搜索,当拆分的前缀字符串在字典中存在时,从i之后的字符串再去拆分查找,如果后面的字符串拆分都能查询,则返回true;如果后面的单词拆分不能找到对应的,i++,前缀单词增加一个字符,继续去查后面的字符串,直到startIndex=s.length

优化重复查找,声明history变量存在某个位置(i)的字符串是否存在,从history查询到i之前的前缀字符串已经存在/不存在时,就可以直接返回

export function wordBreak(s: string, wordDict: string[]): boolean {
  const length = s.length;
  const dict = new Set(wordDict);
  const history: boolean[] = Array.from({ length });

  const dfs = (startIndex: number): boolean => {
    if (startIndex === length) return true;
    if (history[startIndex] !== undefined) return history[startIndex];
    for (let i = startIndex + 1; i <= length; i++) {
      const prefix = s.slice(startIndex, i);
      if (dict.has(prefix) && dfs(i)) {
        history[i] = true;
        return true;
      }
    }
    history[startIndex] = false;
    return false;
  };

  return dfs(0);
}