sweet1cris

Untitled

Feb 9th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1.  
  2. // version 1:
  3. public class Solution {
  4.     private void search(int index, String s, List<Integer> path,
  5.                    boolean[][] isWord, boolean[] possible,
  6.                    List<String> result) {
  7.         if (!possible[index]) {
  8.             return;
  9.         }
  10.        
  11.         if (index == s.length()) {
  12.             StringBuilder sb = new StringBuilder();
  13.             int lastIndex = 0;
  14.             for (int i = 0; i < path.size(); i++) {
  15.                 sb.append(s.substring(lastIndex, path.get(i)));
  16.                 if (i != path.size() - 1) sb.append(" ");
  17.                 lastIndex = path.get(i);
  18.             }
  19.             result.add(sb.toString());
  20.             return;
  21.         }
  22.        
  23.         for (int i = index; i < s.length(); i++) {
  24.             if (!isWord[index][i]) {
  25.                 continue;
  26.             }
  27.             path.add(i + 1);
  28.             search(i + 1, s, path, isWord, possible, result);
  29.             path.remove(path.size() - 1);
  30.         }
  31.     }
  32.    
  33.     public List<String> wordBreak(String s, Set<String> wordDict) {
  34.         ArrayList<String> result = new ArrayList<String>();
  35.         if (s.length() == 0) {
  36.             return result;
  37.         }
  38.        
  39.         boolean[][] isWord = new boolean[s.length()][s.length()];
  40.         for (int i = 0; i < s.length(); i++) {
  41.             for (int j = i; j < s.length(); j++) {
  42.                 String word = s.substring(i, j + 1);
  43.                 isWord[i][j] = wordDict.contains(word);
  44.             }
  45.         }
  46.        
  47.         boolean[] possible = new boolean[s.length() + 1];
  48.         possible[s.length()] = true;
  49.         for (int i = s.length() - 1; i >= 0; i--) {
  50.             for (int j = i; j < s.length(); j++) {
  51.                 if (isWord[i][j] && possible[j + 1]) {
  52.                     possible[i] = true;
  53.                     break;
  54.                 }
  55.             }
  56.         }
  57.        
  58.         List<Integer> path = new ArrayList<Integer>();
  59.         search(0, s, path, isWord, possible, result);
  60.         return result;
  61.     }
  62. }
  63.  
  64. // version 2:
  65.  
  66. public class Solution {
  67.     public ArrayList<String> wordBreak(String s, Set<String> dict) {
  68.         // Note: The Solution object is instantiated only once and is reused by each test case.
  69.         Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
  70.         return wordBreakHelper(s,dict,map);
  71.     }
  72.  
  73.     public ArrayList<String> wordBreakHelper(String s, Set<String> dict, Map<String, ArrayList<String>> memo){
  74.         if(memo.containsKey(s)) return memo.get(s);
  75.         ArrayList<String> result = new ArrayList<String>();
  76.         int n = s.length();
  77.         if(n <= 0) return result;
  78.         for(int len = 1; len <= n; ++len){
  79.             String subfix = s.substring(0,len);
  80.             if(dict.contains(subfix)){
  81.                 if(len == n){
  82.                     result.add(subfix);
  83.                 }else{
  84.                     String prefix = s.substring(len);
  85.                     ArrayList<String> tmp = wordBreakHelper(prefix, dict, memo);
  86.                     for(String item:tmp){
  87.                         item = subfix + " " + item;
  88.                         result.add(item);
  89.                     }
  90.                 }
  91.             }
  92.         }
  93.         memo.put(s, result);
  94.         return result;
  95.     }
  96. }
  97.  
  98.  
  99. //version:高频题班
  100. public class Solution {
  101.     Map<String, List<String>> done = new HashMap<>();
  102.     Set<String> dict;
  103.  
  104.     public List<String> wordBreak(String s, Set<String> dict) {
  105.         this.dict = dict;
  106.         done.put("", new ArrayList<>());
  107.         done.get("").add("");
  108.  
  109.         return dfs(s);
  110.     }
  111.  
  112.     List<String> dfs(String s) {
  113.         if (done.containsKey(s)) {
  114.             return done.get(s);
  115.         }
  116.         List<String> ans = new ArrayList<>();
  117.  
  118.         for (int len = 1; len <= s.length(); len++) {  //将s 分割成s1 s2  其中s1长度为len
  119.             String s1 = s.substring(0, len);
  120.             String s2 = s.substring(len);
  121.  
  122.             if (dict.contains(s1)) {
  123.                 List<String> s2_res = dfs(s2);
  124.                 for (String item : s2_res) {
  125.                     if (item == "") {
  126.                         ans.add(s1);
  127.                     } else {
  128.                         ans.add(s1 + " " + item);
  129.                     }
  130.                 }
  131.             }
  132.         }
  133.         done.put(s, ans);
  134.         return ans;
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment