|
| 1 | +/** |
| 2 | + input : string s and array of strings |
| 3 | + output : return true if s can be segmented into one or more words in dictionary |
| 4 | + constraints : |
| 5 | + 1) same word can be used multiple times |
| 6 | + 2) every word in dictionary is unique |
| 7 | + 3) s.length [1, 300] |
| 8 | + 4) length of tokens in dictionary [1, 20] |
| 9 | +
|
| 10 | + ex) |
| 11 | + leetcode >> leet code |
| 12 | + applepenapple >> apple pen |
| 13 | + applepenapple >> apple applepen |
| 14 | +
|
| 15 | +
|
| 16 | + solution 1) bruteforce |
| 17 | + for each character c, check if word exists in dictionary |
| 18 | + that the first letter is c |
| 19 | +
|
| 20 | + match string |
| 21 | + move to next |
| 22 | + unmatch |
| 23 | + next word |
| 24 | +
|
| 25 | + >> get all the combinations |
| 26 | + O(n * m * k) |
| 27 | + n is the length of string s, |
| 28 | + m is number of tokens, |
| 29 | + k is length of token |
| 30 | +
|
| 31 | + tc : O (nmk) (300 * 1000 * 20) ~= 6 * 10^6 < 1sec |
| 32 | + sc : O (mk) |
| 33 | +
|
| 34 | + solution 2) optimize? heuristic? |
| 35 | +
|
| 36 | + using trie will reduce tc |
| 37 | + for each character: |
| 38 | + get all substring substr(i, j) |
| 39 | + search trie |
| 40 | +
|
| 41 | + tc : O(n^2 + m*k) |
| 42 | +
|
| 43 | + solution 3) dp |
| 44 | +
|
| 45 | + let dp[i] true if possible to build s which length is i |
| 46 | + with segemented tokens |
| 47 | +
|
| 48 | + build HashSet for tokens |
| 49 | + iterate i from 1 to n |
| 50 | + iterate j from 0 to i-1 |
| 51 | + dp[i] = dp[j] & substr(j,i) presents // substr O(n) |
| 52 | + if true break; |
| 53 | +
|
| 54 | + tc : O(n^3 + m*k) |
| 55 | + */ |
| 56 | +class Solution { |
| 57 | + public boolean wordBreak(String s, List<String> wordDict) { |
| 58 | + Set<String> dict = new HashSet<>(wordDict); |
| 59 | + boolean[] dp = new boolean[s.length() + 1]; |
| 60 | + dp[0] = true; |
| 61 | + for(int i = 1; i < dp.length; i++) { |
| 62 | + for(int j = 0; j < i; j++) { |
| 63 | + if(dp[j] && dict.contains(s.substring(j, i))){ |
| 64 | + dp[i] = true; |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + return dp[dp.length - 1]; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +// class Solution1 { |
| 75 | +// public boolean wordBreak(String s, List<String> wordDict) { |
| 76 | +// Map<Character, List<String>> tokens = new HashMap<>(); |
| 77 | +// for(String word : wordDict) { |
| 78 | +// tokens.putIfAbsent(word.charAt(word.length()-1), new ArrayList<>()); |
| 79 | +// tokens.get(word.charAt(word.length()-1)).add(word); |
| 80 | +// } |
| 81 | +// boolean[] dp = new boolean[s.length()+1]; |
| 82 | +// dp[0] = true; |
| 83 | +// for(int i = 1; i < dp.length; i++) { |
| 84 | +// List<String> token = tokens.get(s.charAt(i-1)); |
| 85 | +// if(token == null) {dp[i] = false; continue;} |
| 86 | +// for(String word : token) { |
| 87 | +// if(i - word.length() < 0) continue; |
| 88 | +// dp[i] = dp[i-word.length()] && s.substring(i-word.length(), i).equals(word); |
| 89 | +// if(dp[i]) break; |
| 90 | +// } |
| 91 | +// } |
| 92 | +// return dp[dp.length-1]; |
| 93 | +// } |
| 94 | +// } |
0 commit comments