Skip to content

Commit 9dea544

Browse files
refactor 411
1 parent 69e2401 commit 9dea544

File tree

1 file changed

+68
-65
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+68
-65
lines changed

src/main/java/com/fishercoder/solutions/_411.java

Lines changed: 68 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -29,91 +29,94 @@
2929
*
3030
*/
3131
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+
}
3738

38-
Trie root = new Trie();
39-
List<String> abbrs;
39+
Trie root = new Trie();
40+
List<String> abbrs;
4041

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+
}
4546

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+
}
5254
}
5355
}
56+
return "";
5457
}
55-
return "";
56-
}
5758

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'];
6467
}
65-
cur = cur.children[c - 'a'];
68+
cur.isWord = true;
6669
}
67-
cur.isWord = true;
68-
}
6970

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+
}
7475

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+
}
7981
}
82+
return false;
8083
}
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;
8689
}
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);
9499
}
95-
return search(target, root, i, tmp);
96-
} else {
97-
return search(target, root.children[target.charAt(i) - 'a'], i + 1, 0);
98100
}
99-
}
100101

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;
105111
}
106-
if (num == 1 && abbr != 0) {
107-
abbrs.add(tmp + abbr);
112+
if (num <= 0) {
113+
return;
108114
}
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);
113119
}
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);
117120
}
118121

119122
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy