|
29 | 29 | *
|
30 | 30 | */
|
31 | 31 | public class _411 {
|
32 |
| - /**Credit: https://discuss.leetcode.com/topic/61346/trie-bruteforce*/ |
33 |
| - class Trie { |
34 |
| - Trie[] children = new Trie[26]; |
35 |
| - boolean isWord = false; |
36 |
| - } |
| 32 | + public static class Solution1 { |
| 33 | + /** Credit: https://discuss.leetcode.com/topic/61346/trie-bruteforce */ |
| 34 | + class Trie { |
| 35 | + Trie[] children = new Trie[26]; |
| 36 | + boolean isWord = false; |
| 37 | + } |
37 | 38 |
|
38 |
| - Trie root = new Trie(); |
39 |
| - List<String> abbrs; |
| 39 | + Trie root = new Trie(); |
| 40 | + List<String> abbrs; |
40 | 41 |
|
41 |
| - public String minAbbreviation(String target, String[] dictionary) { |
42 |
| - for (String s : dictionary) { |
43 |
| - addTrie(s); |
44 |
| - } |
| 42 | + public String minAbbreviation(String target, String[] dictionary) { |
| 43 | + for (String s : dictionary) { |
| 44 | + addTrie(s); |
| 45 | + } |
45 | 46 |
|
46 |
| - for (int i = 0; i < target.length(); i++) { |
47 |
| - abbrs = new ArrayList<>(); |
48 |
| - abbrGenerator(target, 0, "", 0, i + 1); |
49 |
| - for (String s : abbrs) { |
50 |
| - if (search(s, root, 0, 0) == false) { |
51 |
| - return s; |
| 47 | + for (int i = 0; i < target.length(); i++) { |
| 48 | + abbrs = new ArrayList<>(); |
| 49 | + abbrGenerator(target, 0, "", 0, i + 1); |
| 50 | + for (String s : abbrs) { |
| 51 | + if (search(s, root, 0, 0) == false) { |
| 52 | + return s; |
| 53 | + } |
52 | 54 | }
|
53 | 55 | }
|
| 56 | + return ""; |
54 | 57 | }
|
55 |
| - return ""; |
56 |
| - } |
57 | 58 |
|
58 |
| - public void addTrie(String s) { |
59 |
| - Trie cur = root; |
60 |
| - for (int i = 0; i < s.length(); i++) { |
61 |
| - char c = s.charAt(i); |
62 |
| - if (cur.children[c - 'a'] == null) { |
63 |
| - cur.children[c - 'a'] = new Trie(); |
| 59 | + public void addTrie(String s) { |
| 60 | + Trie cur = root; |
| 61 | + for (int i = 0; i < s.length(); i++) { |
| 62 | + char c = s.charAt(i); |
| 63 | + if (cur.children[c - 'a'] == null) { |
| 64 | + cur.children[c - 'a'] = new Trie(); |
| 65 | + } |
| 66 | + cur = cur.children[c - 'a']; |
64 | 67 | }
|
65 |
| - cur = cur.children[c - 'a']; |
| 68 | + cur.isWord = true; |
66 | 69 | }
|
67 |
| - cur.isWord = true; |
68 |
| - } |
69 | 70 |
|
70 |
| - public boolean search(String target, Trie root, int i, int loop) { |
71 |
| - if (root == null) { |
72 |
| - return false; |
73 |
| - } |
| 71 | + public boolean search(String target, Trie root, int i, int loop) { |
| 72 | + if (root == null) { |
| 73 | + return false; |
| 74 | + } |
74 | 75 |
|
75 |
| - if (loop != 0) { |
76 |
| - for (int a = 0; a < 26; a++) { |
77 |
| - if (search(target, root.children[a], i, loop - 1)) { |
78 |
| - return true; |
| 76 | + if (loop != 0) { |
| 77 | + for (int a = 0; a < 26; a++) { |
| 78 | + if (search(target, root.children[a], i, loop - 1)) { |
| 79 | + return true; |
| 80 | + } |
79 | 81 | }
|
| 82 | + return false; |
80 | 83 | }
|
81 |
| - return false; |
82 |
| - } |
83 |
| - if (i == target.length()) { |
84 |
| - if (root.isWord) { |
85 |
| - return true; |
| 84 | + if (i == target.length()) { |
| 85 | + if (root.isWord) { |
| 86 | + return true; |
| 87 | + } |
| 88 | + return false; |
86 | 89 | }
|
87 |
| - return false; |
88 |
| - } |
89 |
| - if (Character.isDigit(target.charAt(i))) { |
90 |
| - int tmp = 0; |
91 |
| - while (i < target.length() && Character.isDigit(target.charAt(i))) { |
92 |
| - tmp = tmp * 10 + target.charAt(i) - '0'; |
93 |
| - i++; |
| 90 | + if (Character.isDigit(target.charAt(i))) { |
| 91 | + int tmp = 0; |
| 92 | + while (i < target.length() && Character.isDigit(target.charAt(i))) { |
| 93 | + tmp = tmp * 10 + target.charAt(i) - '0'; |
| 94 | + i++; |
| 95 | + } |
| 96 | + return search(target, root, i, tmp); |
| 97 | + } else { |
| 98 | + return search(target, root.children[target.charAt(i) - 'a'], i + 1, 0); |
94 | 99 | }
|
95 |
| - return search(target, root, i, tmp); |
96 |
| - } else { |
97 |
| - return search(target, root.children[target.charAt(i) - 'a'], i + 1, 0); |
98 | 100 | }
|
99 |
| - } |
100 | 101 |
|
101 |
| - public void abbrGenerator(String target, int i, String tmp, int abbr, int num) { |
102 |
| - if (i == target.length()) { |
103 |
| - if (num == 0 && abbr == 0) { |
104 |
| - abbrs.add(tmp); |
| 102 | + public void abbrGenerator(String target, int i, String tmp, int abbr, int num) { |
| 103 | + if (i == target.length()) { |
| 104 | + if (num == 0 && abbr == 0) { |
| 105 | + abbrs.add(tmp); |
| 106 | + } |
| 107 | + if (num == 1 && abbr != 0) { |
| 108 | + abbrs.add(tmp + abbr); |
| 109 | + } |
| 110 | + return; |
105 | 111 | }
|
106 |
| - if (num == 1 && abbr != 0) { |
107 |
| - abbrs.add(tmp + abbr); |
| 112 | + if (num <= 0) { |
| 113 | + return; |
108 | 114 | }
|
109 |
| - return; |
110 |
| - } |
111 |
| - if (num <= 0) { |
112 |
| - return; |
| 115 | + char cur = target.charAt(i); |
| 116 | + abbrGenerator(target, i + 1, abbr == 0 ? tmp + cur : tmp + abbr + cur, 0, |
| 117 | + abbr == 0 ? num - 1 : num - 2); |
| 118 | + abbrGenerator(target, i + 1, tmp, abbr + 1, num); |
113 | 119 | }
|
114 |
| - char cur = target.charAt(i); |
115 |
| - abbrGenerator(target, i + 1, abbr == 0 ? tmp + cur : tmp + abbr + cur, 0, abbr == 0 ? num - 1 : num - 2); |
116 |
| - abbrGenerator(target, i + 1, tmp, abbr + 1, num); |
117 | 120 | }
|
118 | 121 |
|
119 | 122 | }
|
0 commit comments