leetcode 140. Word Break II ----- java

來源:互聯網
上載者:User

標籤:val   arraylist   str   res   div   possible   count   const   for   

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.

Return all such possible sentences.

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

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

 

139題的延伸題,需要的出所有結果。

 

1、遞迴,仍舊逾時。

public class Solution {    String[] result;    List list;    public List<String> wordBreak(String s, Set<String> wordDict) {        int len = s.length(),maxLen = 0;        result = new String[len];        list = new ArrayList<String>();        for( String str : wordDict ){            maxLen = Math.max(maxLen,str.length());        }        helper(s,0,wordDict,maxLen,0);        return list;        }    public void helper(String s,int pos,Set<String> wordDict,int maxLen,int count){        if( pos == s.length() ){            String str = result[0];            for( int i = 1 ; i<count-1;i++){                str =str+ " "+result[i];            }            if( count > 1)                str = str+" "+result[count-1];            list.add(str);        }        int flag = 0;        for( int i = pos;pos - i<maxLen && i<s.length();i++){            if( wordDict.contains( s.substring(pos,i+1) )){                result[count] = s.substring(pos,i+1);                helper(s,i+1,wordDict,maxLen,count+1);                flag = 1;            }        }        }}

 2、加入提前判斷是否存在答案(即上一題的結論)就可以了。

public class Solution {    String[] result;    List list;    public List<String> wordBreak(String s, Set<String> wordDict) {        int len = s.length(),maxLen = 0;        result = new String[len];        list = new ArrayList<String>();                for( String str : wordDict ){            maxLen = Math.max(maxLen,str.length());        }        boolean[] dp = new boolean[len];        for( int i = 0 ;i<len;i++){            for( int j = i;j>=0 && i-j<maxLen;j-- ){                if( ( j == 0 || dp[j-1] == true ) && wordDict.contains(s.substring(j,i+1)) ){                    dp[i] = true;                    break;                }            }        }        if( dp[len-1] == false)            return list;                helper(s,0,wordDict,maxLen,0);        return list;        }    public void helper(String s,int pos,Set<String> wordDict,int maxLen,int count){        if( pos == s.length() ){            String str = result[0];            for( int i = 1 ; i<count-1;i++){                str =str+ " "+result[i];            }            if( count > 1)                str = str+" "+result[count-1];            list.add(str);        }        int flag = 0;        for( int i = pos;pos - i<maxLen && i<s.length();i++){            if( wordDict.contains( s.substring(pos,i+1) )){                result[count] = s.substring(pos,i+1);                helper(s,i+1,wordDict,maxLen,count+1);                flag = 1;            }        }        }}

 

leetcode 140. Word Break II ----- java

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.