Skip to content

Commit 15afb22

Browse files
committed
feat: add solutions to leetcode problem: No.0288. Unique Word Abbreviation
1 parent 727a27a commit 15afb22

File tree

4 files changed

+182
-6
lines changed

4 files changed

+182
-6
lines changed

solution/0200-0299/0288.Unique Word Abbreviation/README.md

+62-3
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,86 @@ validWordAbbr.isUnique("cake"); // 返回 true,因为 "cake" 已经存在于
6363
<li>最多调用 <code>5000</code> 次 <code>isUnique</code></li>
6464
</ul>
6565

66-
6766
## 解法
6867

6968
<!-- 这里可写通用的实现逻辑 -->
7069

70+
哈希表实现,其中 key 存放单词缩写,value 存放单词缩写所对应的所有单词的集合。
71+
7172
<!-- tabs:start -->
7273

7374
### **Python3**
7475

7576
<!-- 这里可写当前语言的特殊实现逻辑 -->
7677

7778
```python
78-
79+
class ValidWordAbbr:
80+
81+
def __init__(self, dictionary: List[str]):
82+
self.words = {}
83+
for word in dictionary:
84+
abbr = self._word_abbr(word)
85+
vals = self.words.get(abbr, set())
86+
vals.add(word)
87+
self.words[abbr] = vals
88+
89+
def isUnique(self, word: str) -> bool:
90+
abbr = self._word_abbr(word)
91+
vals = self.words.get(abbr)
92+
return vals is None or (len(vals) == 1 and word in vals)
93+
94+
def _word_abbr(self, word: str) -> str:
95+
n = len(word)
96+
if n < 3:
97+
return word
98+
return f'{word[0]}{n - 2}{word[n - 1]}'
99+
100+
101+
# Your ValidWordAbbr object will be instantiated and called as such:
102+
# obj = ValidWordAbbr(dictionary)
103+
# param_1 = obj.isUnique(word)
79104
```
80105

81106
### **Java**
82107

83108
<!-- 这里可写当前语言的特殊实现逻辑 -->
84109

85110
```java
86-
111+
class ValidWordAbbr {
112+
private Map<String, Set<String>> words;
113+
114+
public ValidWordAbbr(String[] dictionary) {
115+
words = new HashMap<>();
116+
for (String word : dictionary) {
117+
String abbr = wordAbbr(word);
118+
Set<String> vals = words.getOrDefault(abbr, new HashSet<>());
119+
vals.add(word);
120+
words.put(abbr, vals);
121+
}
122+
}
123+
124+
public boolean isUnique(String word) {
125+
String abbr = wordAbbr(word);
126+
Set<String> vals = words.get(abbr);
127+
return vals == null || (vals.size() == 1 && vals.contains(word));
128+
}
129+
130+
private String wordAbbr(String word) {
131+
int n = word.length();
132+
if (n < 3) {
133+
return word;
134+
}
135+
StringBuilder sb = new StringBuilder();
136+
sb.append(word.charAt(0)).append(n - 2).append(word.charAt(n - 1));
137+
return sb.toString();
138+
}
139+
}
140+
141+
/**
142+
* Your ValidWordAbbr object will be instantiated and called as such:
143+
* ValidWordAbbr obj = new ValidWordAbbr(dictionary);
144+
* boolean param_1 = obj.isUnique(word);
145+
*/
87146
```
88147

89148
### **...**

solution/0200-0299/0288.Unique Word Abbreviation/README_EN.md

+60-3
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,78 @@ validWordAbbr.isUnique(&quot;cake&quot;); // return true, because &quot;cake&quo
5959
<li>At most <code>5000</code> calls will be made to <code>isUnique</code>.</li>
6060
</ul>
6161

62-
6362
## Solutions
6463

6564
<!-- tabs:start -->
6665

6766
### **Python3**
6867

6968
```python
70-
69+
class ValidWordAbbr:
70+
71+
def __init__(self, dictionary: List[str]):
72+
self.words = {}
73+
for word in dictionary:
74+
abbr = self._word_abbr(word)
75+
vals = self.words.get(abbr, set())
76+
vals.add(word)
77+
self.words[abbr] = vals
78+
79+
def isUnique(self, word: str) -> bool:
80+
abbr = self._word_abbr(word)
81+
vals = self.words.get(abbr)
82+
return vals is None or (len(vals) == 1 and word in vals)
83+
84+
def _word_abbr(self, word: str) -> str:
85+
n = len(word)
86+
if n < 3:
87+
return word
88+
return f'{word[0]}{n - 2}{word[n - 1]}'
89+
90+
91+
# Your ValidWordAbbr object will be instantiated and called as such:
92+
# obj = ValidWordAbbr(dictionary)
93+
# param_1 = obj.isUnique(word)
7194
```
7295

7396
### **Java**
7497

7598
```java
76-
99+
class ValidWordAbbr {
100+
private Map<String, Set<String>> words;
101+
102+
public ValidWordAbbr(String[] dictionary) {
103+
words = new HashMap<>();
104+
for (String word : dictionary) {
105+
String abbr = wordAbbr(word);
106+
Set<String> vals = words.getOrDefault(abbr, new HashSet<>());
107+
vals.add(word);
108+
words.put(abbr, vals);
109+
}
110+
}
111+
112+
public boolean isUnique(String word) {
113+
String abbr = wordAbbr(word);
114+
Set<String> vals = words.get(abbr);
115+
return vals == null || (vals.size() == 1 && vals.contains(word));
116+
}
117+
118+
private String wordAbbr(String word) {
119+
int n = word.length();
120+
if (n < 3) {
121+
return word;
122+
}
123+
StringBuilder sb = new StringBuilder();
124+
sb.append(word.charAt(0)).append(n - 2).append(word.charAt(n - 1));
125+
return sb.toString();
126+
}
127+
}
128+
129+
/**
130+
* Your ValidWordAbbr object will be instantiated and called as such:
131+
* ValidWordAbbr obj = new ValidWordAbbr(dictionary);
132+
* boolean param_1 = obj.isUnique(word);
133+
*/
77134
```
78135

79136
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class ValidWordAbbr {
2+
private Map<String, Set<String>> words;
3+
4+
public ValidWordAbbr(String[] dictionary) {
5+
words = new HashMap<>();
6+
for (String word : dictionary) {
7+
String abbr = wordAbbr(word);
8+
Set<String> vals = words.getOrDefault(abbr, new HashSet<>());
9+
vals.add(word);
10+
words.put(abbr, vals);
11+
}
12+
}
13+
14+
public boolean isUnique(String word) {
15+
String abbr = wordAbbr(word);
16+
Set<String> vals = words.get(abbr);
17+
return vals == null || (vals.size() == 1 && vals.contains(word));
18+
}
19+
20+
private String wordAbbr(String word) {
21+
int n = word.length();
22+
if (n < 3) {
23+
return word;
24+
}
25+
StringBuilder sb = new StringBuilder();
26+
sb.append(word.charAt(0)).append(n - 2).append(word.charAt(n - 1));
27+
return sb.toString();
28+
}
29+
}
30+
31+
/**
32+
* Your ValidWordAbbr object will be instantiated and called as such:
33+
* ValidWordAbbr obj = new ValidWordAbbr(dictionary);
34+
* boolean param_1 = obj.isUnique(word);
35+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class ValidWordAbbr:
2+
3+
def __init__(self, dictionary: List[str]):
4+
self.words = {}
5+
for word in dictionary:
6+
abbr = self._word_abbr(word)
7+
vals = self.words.get(abbr, set())
8+
vals.add(word)
9+
self.words[abbr] = vals
10+
11+
def isUnique(self, word: str) -> bool:
12+
abbr = self._word_abbr(word)
13+
vals = self.words.get(abbr)
14+
return vals is None or (len(vals) == 1 and word in vals)
15+
16+
def _word_abbr(self, word: str) -> str:
17+
n = len(word)
18+
if n < 3:
19+
return word
20+
return f'{word[0]}{n - 2}{word[n - 1]}'
21+
22+
23+
# Your ValidWordAbbr object will be instantiated and called as such:
24+
# obj = ValidWordAbbr(dictionary)
25+
# param_1 = obj.isUnique(word)

0 commit comments

Comments
 (0)