leetcode算法练习8_单词拆分 II

给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中。返回所有这些可能的句子。

说明:

拆分时可以重复使用字典中的单词。
你可以假设字典中没有重复的单词。

示例 1:

输入:
s = “catsanddog”
wordDict = [“cat”, “cats”, “and”, “sand”, “dog”]

输出:
[
“cats and dog”,
“cat sand dog”
]

示例 2:

输入:
s = “pineapplepenapple”
wordDict = [“apple”, “pen”, “applepen”, “pine”, “pineapple”]

输出:
[
“pine apple pen apple”,
“pineapple pen apple”,
“pine applepen apple”
]

解析:

解题思路就是 DFS,用一个boolean[] 数组记录当前可分割的位置,每一次深搜的过程都是找下一个可以分割的位置。

可以简单的将s = “aab” ,wordDict=[“a”,“b”,“aa”],代入单步调试,可以方便清晰的看出解题思路

并且先使用单词拆分1(上一篇的单词拆分)中的算法先判断,避免不必要的资源消耗(主要是因为LeetCode中有一个个错误的测试用例会报超时异常,这是汇集评论区里各位人才总结出的最简单的解决方案o( ̄︶ ̄)o)。

为了更容易理解,通过代码简单输出执行的流程(代码中 注释掉的输出部分),建议自己捋清楚整个算法的执行流程,以便举一反三,下面给出我整理的流程(只给思路,不涉及分割位置数组的细节)。

假设对于 s=”aab” 而言,就是三层递归,每层递归向后找能分割的位置:

层1:从下标 0~2 寻找分割位置,对应子串为 aab
层2:从下标 x~2(x为 最外层传入的下标 和 1 中的大值) 寻找分割位置,对应子串为 ab 或 b
层3:从下标 y~2(y为 中间层传入的下标 和 2 中的大者 ) 寻找分割位置,对应子串为 b 或 无
流程:
先使用个数少的“aab”来测试。
假设wordDict中有[“a”,“b”,“aa”],

  1. 最开始会深搜到 层3,将 s 分割为 “a”,”a”,”b”,并记录此分割方案 [“a”,”a”,”b”]
  2. 回溯到 层2, “a” 不动,判断 “ab” 不满足(即考虑 [“a”,”ab”] 方案)
  3. 回溯到 层1,判断”aa” 满足
  4. 进入 层2,判断 “b” 满足,此时记录此种方案 [“aa”,”b”]
  5. 回溯到 层1,”aa”的基础上加上 “b”,判断 “aab” 不满足
  6. 层1 遍历结束,整个流程结束。

代码:

class Solution {
class Solution {
    List results;
    public List<String> wordBreak(String s, List<String> wordDict) {
        results = new ArrayList<String>();
        if(wordBreakBo(s,wordDict)){
            boolean[] cut = new boolean[s.length()];
            find(s,0,cut,wordDict);
        }
        return results;
    }
    
     private boolean wordBreakBo(String s, List<String> wordDict) {//为单词拆分1中的算法,详情见上一篇
         int a = s.length();
        boolean [] net = new boolean[a+1];
        net[0] = true;
        for(int i = 1;i <= a;i++){
            for(int j = i-1; j>=0&& !net[i];j--){
                String temp = s.substring(j,i);
                net[i] = net[j]&&wordDict.contains(temp);
            }
        }
        return net[a];
    }

    private void find(String s,int index,boolean[] cut,List<String> wordDict){

        if(index == s.length()){//一旦可以执行到最后一步,就说明得出了完整的切割数组数组,可以根据cut来分割回文数。
            String temp = "";
            int begin = 0,last = 0;
            while(last < s.length()){
                if(cut[last]){
                    temp +=s.substring(begin,last+1);
                    if(last < s.length()-1){//最后一位不需要加入空格
                        temp += " ";
                    }
                    begin = last+1;
                }
                last++;
            }
            results.add(temp);

        }
        //向右找符合字符串,当且仅当最后一位cut为true时,才可以证明该字符串的确可以使用该wordDict分割。
        for(int i=index;i<s.length();i++){
            if(isPalindrome(s,index,i,wordDict)){
                cut[i] = true;
                //System.out.println("进入下一层");
                //System.out.println("index:"+index+";i:"+i);
                //System.out.println("cut:"+cut[0]+","+cut[1]+","+cut[2]);

                find(s,i+1,cut,wordDict);
                cut[i]=false;

				//System.out.println("返回上一层");
                //System.out.println("index:"+index+";i:"+i);
                //System.out.println("cut:"+cut[0]+","+cut[1]+","+cut[2]);

            }
        }
    }


    //判断是否为存在该字符串
    private boolean isPalindrome(String s,int begin,int last,List<String> wordDict){
        String te = s.substring(begin,last+1);
        return wordDict.contains(te);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值