首页 > 其他 > 详细

leetcode--Word Break II

时间:2014-02-28 19:25:25      阅读:428      评论:0      收藏:0      [点我收藏+]

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.

Retun all such possible sentences.

For example, given
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"].

A solution is ["cats and dog", "cat sand dog"].

 

Have you been asked this question in an interview? 

 

In this post, I used a very stupid method to solve this problem:

1. Using the dynamic programming method to find all possible position to cut the string(The same as Word Break)

This step will return a boolean matrix, if(i,j) entry is true then the substring s.substring(i,j) can be splitted.

2. Using the boolean matrix obtained in step1, we do DFS to obtain all strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class Solution {
    public ArrayList<String> wordBreak(String s, Set<String> dict) {
        ArrayList<String> result = new ArrayList<String>();
        if(s == null || dict.size() == 0)
            return result;
        //find the position to split the string
        int len = s.length();
        boolean[][] splits = new boolean[len][len + 1];
        for(int i = len - 1; i >= 0; --i){
            splits[i][i] = false;
            for(int j = i + 1; j < len + 1; ++j){
                if(dict.contains(s.substring(i, j)))
                    splits[i][j] = true;
                else{
                    for(int k = i; k < j; ++k){
                        splits[i][j] = splits[i][k] && splits[k][j];
                        if(splits[i][j])
                            break;
                    }
                }
            }
        }
        //find the possible splitting of the string
        //I use dfs method to solve this.
        if(splits[0][len]){
            ArrayList<ArrayList<Integer>> resultInPosition = new ArrayList<ArrayList<Integer>>();
            HashMap<Integer, ArrayList<ArrayList<Integer>> > finished = new HashMap<Integer,ArrayList<ArrayList<Integer>> >();
                for(int i = len - 1; i >= 0; --i){
                ArrayList<ArrayList<Integer>> startAti = new ArrayList<ArrayList<Integer>>();
                if(splits[i][len]){
                    if(dict.contains(s.substring(i, len))){
                        ArrayList<Integer> aList = new ArrayList<Integer>();
                        aList.add(len);
                        startAti.add(aList);
                    }
                    for(int j = i + 1; j < len; ++j){
                        if(dict.contains(s.substring(i,j)) && splits[j][len]){
                            ArrayList<ArrayList<Integer> > fin = finished.get(j);
                            for(ArrayList<Integer> ls : fin){
                                ArrayList<Integer> temp = new ArrayList<Integer>();
                                temp.add(j);
                                temp.addAll(ls);
                                startAti.add(temp);
                            }          
                        }      
                    }
                }
                finished.put(i, startAti);
            }
            resultInPosition = finished.get(0);
            for(ArrayList<Integer> ls : resultInPosition){
                int lssize = ls.size();
                    int first = ls.get(0); 
                String resultString = s.substring(0, first);
                for(int i = 1; i < lssize; ++i){
                    resultString += (" "+ s.substring(first, ls.get(i)));
                    first = ls.get(i);
                }
                result.add(resultString);          
            }
                     
        }
        return result;
    }
}

I tested this code on leetcode online judge, it was accepted and it used 450ms for 24 test cases. I think it is ok although it may not be the best.  

 

 

leetcode--Word Break II,布布扣,bubuko.com

leetcode--Word Break II

原文:http://www.cnblogs.com/averillzheng/p/3572752.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!