|
1 | 1 | class Solution {
|
2 |
| - |
3 |
| - public String replaceWords(List<String> dict, String sentence) { |
4 |
| - |
5 |
| - Map<String, Integer> map = new HashMap<>(); |
6 |
| - |
7 |
| - for (String s: dict) { |
8 |
| - map.put(s, s.length()); |
9 |
| - } |
10 |
| - |
11 |
| - Map<String, Integer> sortedMap = sortByValue(map); |
12 |
| - |
13 |
| - String[] arr = sentence.split("\\s"); |
14 |
| - String[] ans = new String[arr.length]; |
15 |
| - int j = 0; |
16 |
| - |
17 |
| - for (String s : arr) { |
18 |
| - boolean gotIt = false; |
19 |
| - for (Map.Entry<String,Integer> entry : sortedMap.entrySet()) { |
20 |
| - if (s.startsWith(entry.getKey())) { |
21 |
| - ans[j] = entry.getKey(); |
22 |
| - gotIt = true; |
23 |
| - break; |
24 |
| - } |
25 |
| - } |
26 |
| - |
27 |
| - if (!gotIt) { |
28 |
| - ans[j] = s; |
29 |
| - } |
30 |
| - |
31 |
| - j++; |
32 |
| - } |
33 |
| - |
34 |
| - StringBuilder ansString = new StringBuilder(""); |
35 |
| - |
36 |
| - for (String s : ans) { |
37 |
| - ansString.append(s + " "); |
38 |
| - } |
39 |
| - |
40 |
| - return ansString.toString().trim(); |
| 2 | + public String replaceWords(List<String> dict, String sentence) { |
| 3 | + Node root = new Node('-'); |
| 4 | + for (String word : dict) { |
| 5 | + addWord(word, 0, root); |
41 | 6 | }
|
42 |
| - |
43 |
| - private Map<String, Integer> sortByValue(Map<String, Integer> unsortMap) { |
44 |
| - |
45 |
| - List<Map.Entry<String, Integer>> list = |
46 |
| - new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet()); |
47 |
| - |
48 |
| - Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() { |
49 |
| - public int compare(Map.Entry<String, Integer> o1, |
50 |
| - Map.Entry<String, Integer> o2) { |
51 |
| - return (o1.getValue()).compareTo(o2.getValue()); |
52 |
| - } |
53 |
| - }); |
54 |
| - |
55 |
| - Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>(); |
56 |
| - for (Map.Entry<String, Integer> entry : list) { |
57 |
| - sortedMap.put(entry.getKey(), entry.getValue()); |
58 |
| - } |
59 |
| - |
60 |
| - return sortedMap; |
| 7 | + String[] words = sentence.split("\\s+"); |
| 8 | + for (int i = 0; i < words.length; i++) { |
| 9 | + String prefix = getPrefix(words[i], root); |
| 10 | + if (prefix != null) { |
| 11 | + words[i] = prefix; |
| 12 | + } |
| 13 | + } |
| 14 | + return String.join(" ", words); |
| 15 | + } |
| 16 | + |
| 17 | + private String getPrefix(String word, Node root) { |
| 18 | + for (int i = 0; i < word.length(); i++) { |
| 19 | + if (!root.children.containsKey(word.charAt(i))) { |
| 20 | + return null; |
| 21 | + } |
| 22 | + root = root.children.get(word.charAt(i)); |
| 23 | + if (root.word != null) { |
| 24 | + return root.word; |
| 25 | + } |
61 | 26 | }
|
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + private void addWord(String word, int idx, Node root) { |
| 31 | + if (idx == word.length()) { |
| 32 | + return; |
| 33 | + } |
| 34 | + if (!root.children.containsKey(word.charAt(idx))) { |
| 35 | + root.children.put(word.charAt(idx), new Node(word.charAt(idx))); |
| 36 | + } |
| 37 | + root = root.children.get(word.charAt(idx)); |
| 38 | + if (idx == word.length() - 1) { |
| 39 | + root.word = word; |
| 40 | + } |
| 41 | + addWord(word, idx + 1, root); |
| 42 | + } |
| 43 | +} |
| 44 | + |
62 | 45 |
|
| 46 | +class Node { |
| 47 | + char c; |
| 48 | + Map<Character, Node> children; |
| 49 | + String word; |
| 50 | + |
| 51 | + public Node(char c) { |
| 52 | + this.c = c; |
| 53 | + children = new HashMap<>(); |
| 54 | + word = null; |
| 55 | + } |
63 | 56 | }
|
0 commit comments