zoukankan      html  css  js  c++  java
  • LeetCode 140. Word Break II

    原题链接在这里:https://leetcode.com/problems/word-break-ii/

    题目:

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences.

    Note:

    • The same word in the dictionary may be reused multiple times in the segmentation.
    • You may assume the dictionary does not contain duplicate words.

    Example 1:

    Input:
    s = "catsanddog"
    wordDict = ["cat", "cats", "and", "sand", "dog"]
    Output:
    [
      "cats and dog",
      "cat sand dog"
    ]
    

    Example 2:

    Input:
    s = "pineapplepenapple"
    wordDict = ["apple", "pen", "applepen", "pine", "pineapple"]
    Output:
    [
      "pine apple pen apple",
      "pineapple pen apple",
      "pine applepen apple"
    ]
    Explanation: Note that you are allowed to reuse a dictionary word.
    

    Example 3:

    Input:
    s = "catsandog"
    wordDict = ["cats", "dog", "sand", "and", "cat"]
    Output:
    []

    题解:

    When it needs all the possible results, it comes to dfs.

    Could use memo to prune branches. Use memo means divide and conquer, not iterative.

    If cache already has key s, then return list value.

    Otherwise, get either head or tail of s, check if it is in the wordDict. If yes, put the rest in the dfs and get intermediate result.

    Iterate intermediate result, append each candidate and add to res.

    Update cache and return res.

    Note: When wordDict contains current s, add it to res. But do NOT return. Since it may cut more possibilities. 

    e.g. "dog" and "dogs" are both in the result. If see "dogs" and return, it cut all the candidates from "dog".

    Time Complexity: exponential.

    Space: O(n). stack space O(n).

    AC  Java:

     1 class Solution {
     2     Map<String, List<String>> cache = new HashMap<>(); 
     3     public List<String> wordBreak(String s, List<String> wordDict) {
     4         List<String> res = new ArrayList<>();
     5         
     6         if(s == null || s.length() == 0){
     7             return res;
     8         }
     9         
    10         if(cache.containsKey(s)){
    11             return cache.get(s);
    12         }
    13         
    14         if(wordDict.contains(s)){
    15             res.add(s);
    16         }
    17         
    18         for(int i = 1; i<s.length(); i++){
    19             String tail = s.substring(i);
    20             if(wordDict.contains(tail)){
    21                 List<String> cans = wordBreak(s.substring(0, i), wordDict);
    22                 for(String can : cans){
    23                     res.add(can + " " + tail);
    24                 }
    25             }
    26         }
    27         
    28         cache.put(s, res);
    29         return res;
    30     }
    31 }

    类似Word Break.

  • 相关阅读:
    我心中的核心组件(可插拔的AOP)~第十三回 实现AOP的拦截组件Unity.Interception
    .NET 使用unity实现依赖注入
    AOP技术基础
    PowerShell 远程管理之 about_Remote_Troubleshooting
    PowerShell远程连接主机进行会话
    PowerShell_零基础自学课程_9_高级主题:静态类和类的操作
    PowerShell_零基础自学课程_8_高级主题:WMI对象和COM组件
    PowerShell 中的目录文件管理
    解决360浏览器兼容模式不兼容,极速模式兼容问题
    reportng之测试报告升级美化
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/4824963.html
Copyright © 2011-2022 走看看