Skip to content

Commit 446b2c8

Browse files
committed
f
1 parent eda8b0a commit 446b2c8

File tree

2 files changed

+39
-63
lines changed

2 files changed

+39
-63
lines changed

solution/src/main/java/com/inuker/solution/ValidWordAbbr.java

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
*/
1010

1111
/**
12-
* 这道题还挺容易错
1312
* 参考https://leetcode.com/articles/unique-word-abbreviation/
1413
*/
1514
public class ValidWordAbbr {
16-
/** 这题容易错的地方是unique的理解,即dic中不存在该word,要排除word
17-
* // 耗时100ms
15+
/**
16+
* 这题容易错的地方是unique的理解,即dic中不存在该word,要排除word
17+
*/
1818
private HashMap<String, Set<String>> mMap;
1919

2020
public ValidWordAbbr(String[] dictionary) {
@@ -31,45 +31,15 @@ public ValidWordAbbr(String[] dictionary) {
3131
}
3232
}
3333

34+
/**
35+
* unique意味着不存在该abbr或存在但是只有该单词一个
36+
*/
3437
public boolean isUnique(String word) {
3538
Set<String> set = mMap.get(getAbbr(word));
3639
return set == null || (set.size() == 1 && set.contains(word));
3740
}
3841

39-
private String getAbbr(String word) {
40-
int len = word.length();
41-
if (len == 0) {
42-
return "";
43-
}
44-
return word.charAt(0) + String.valueOf(len - 2) + word.charAt(len - 1);
45-
}*/
46-
47-
private HashMap<String, Integer> mMap;
48-
private Set<String> mSet;
49-
50-
// 耗时69ms,比上面的快一点
51-
public ValidWordAbbr(String[] dictionary) {
52-
mMap = new HashMap<String, Integer>();
53-
mSet = new HashSet<String>();
54-
for (String text : dictionary) {
55-
String abbr = getAbbr(text);
56-
57-
if (mSet.add(text)) {
58-
mMap.put(abbr, mMap.containsKey(abbr) ? mMap.get(abbr) + 1 : 1);
59-
}
60-
}
61-
}
62-
63-
public boolean isUnique(String word) {
64-
String abbr = getAbbr(word);
65-
return !mMap.containsKey(abbr) || (mMap.get(abbr) == 1 && mSet.contains(word));
66-
}
67-
68-
private String getAbbr(String word) {
69-
int len = word.length();
70-
if (len == 0) {
71-
return "";
72-
}
73-
return word.charAt(0) + String.valueOf(len - 2) + word.charAt(len - 1);
42+
private String getAbbr(String s) {
43+
return s.length() > 2 ? s.substring(0, 1) + (s.length() - 2) + s.substring(s.length() - 1) : s;
7444
}
7545
}

test/src/main/java/com/inuker/test/main.java

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,49 @@
1111
import java.util.Collections;
1212
import java.util.Comparator;
1313
import java.util.Deque;
14+
import java.util.HashMap;
15+
import java.util.HashSet;
1416
import java.util.Iterator;
1517
import java.util.LinkedList;
1618
import java.util.List;
19+
import java.util.Set;
1720
import java.util.Stack;
1821

1922
public class main {
2023

2124
public static void main(String[] args) {
22-
String s = decodeString("2[abc]3[cd]ef");
25+
Codec c = new Codec();
26+
String s = c.encode(Arrays.asList("hello", "how", "are", "you"));
2327
System.out.println(s);
28+
for (String ss : c.decode(s)) {
29+
System.out.println(ss);
30+
}
2431
}
2532

26-
public static String decodeString(String s) {
27-
StringBuilder stack = new StringBuilder();
28-
for (char c : s.toCharArray()) {
29-
if (c != ']') {
30-
stack.append(c);
31-
} else {
32-
StringBuilder sb = new StringBuilder();
33-
while (stack.charAt(stack.length() - 1) != '[') {
34-
sb.insert(0, stack.charAt(stack.length() - 1));
35-
stack.setLength(stack.length() - 1);
36-
}
37-
stack.setLength(stack.length() - 1);
38-
int n = 0, t = 1;
39-
while (stack.length() > 0 && Character.isDigit(stack.charAt(stack.length() - 1))) {
40-
n += t * (stack.charAt(stack.length() - 1) - '0');
41-
t *= 10;
42-
stack.setLength(stack.length() - 1);
43-
}
44-
for (int i = 0; i < n; i++) {
45-
for (int j = 0; j < sb.length(); j++) {
46-
stack.append(sb.charAt(j));
47-
}
48-
}
33+
class ValidWordAbbr {
34+
35+
HashMap<String, Set<String>> map = new HashMap<>();
36+
37+
public ValidWordAbbr(String[] dictionary) {
38+
for (String s : dictionary) {
39+
String abbr = getAbbr(s);
40+
Set<String> set = map.getOrDefault(abbr, new HashSet<String>());
41+
set.add(s);
42+
map.put(abbr, set);
43+
}
44+
}
45+
46+
private String getAbbr(String s) {
47+
return s.length() > 2 ? s.substring(0, 1) + (s.length() - 2) + s.substring(s.length() - 1) : s;
48+
}
49+
50+
public boolean isUnique(String word) {
51+
String abbr = getAbbr(word);
52+
if (!map.containsKey(abbr)) {
53+
return true;
4954
}
55+
Set<String> set = map.get(abbr);
56+
return set.size() == 1 && set.contains(word);
5057
}
51-
return stack.toString();
5258
}
5359
}

0 commit comments

Comments
 (0)