Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 116dea5

Browse files
committed
feat: add solutions to lc problem: No.0288
No.0288.Unique Word Abbreviation
1 parent 49f392a commit 116dea5

File tree

6 files changed

+256
-71
lines changed

6 files changed

+256
-71
lines changed

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

Lines changed: 89 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,18 @@ validWordAbbr.isUnique("cake"); // 返回 true,因为 "cake" 已经存在于
7979
class ValidWordAbbr:
8080

8181
def __init__(self, dictionary: List[str]):
82-
self.words = {}
82+
self.words = defaultdict(set)
8383
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
84+
abbr = self.word_abbr(word)
85+
self.words[abbr].add(word)
8886

8987
def isUnique(self, word: str) -> bool:
90-
abbr = self._word_abbr(word)
91-
vals = self.words.get(abbr)
92-
return vals isNoneor (len(vals) == 1 and word in vals)
88+
abbr = self.word_abbr(word)
89+
words = self.words[abbr]
90+
return not words or (len(words) == 1 and word in words)
9391

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]}'
92+
def word_abbr(self, s):
93+
return s if len(s) < 3 else f'{s[0]}{len(s) - 2}{s[-1]}'
9994

10095

10196
# Your ValidWordAbbr object will be instantiated and called as such:
@@ -114,25 +109,20 @@ class ValidWordAbbr {
114109
public ValidWordAbbr(String[] dictionary) {
115110
words = new HashMap<>();
116111
for (String word : dictionary) {
117-
String abbr = wordAbbr(word);
112+
String abbr = abbr(word);
118113
words.computeIfAbsent(abbr, k -> new HashSet<>()).add(word);
119114
}
120115
}
121-
116+
122117
public boolean isUnique(String word) {
123-
String abbr = wordAbbr(word);
118+
String abbr = abbr(word);
124119
Set<String> vals = words.get(abbr);
125120
return vals == null || (vals.size() == 1 && vals.contains(word));
126121
}
127122

128-
private String wordAbbr(String word) {
129-
int n = word.length();
130-
if (n < 3) {
131-
return word;
132-
}
133-
StringBuilder sb = new StringBuilder();
134-
sb.append(word.charAt(0)).append(n - 2).append(word.charAt(n - 1));
135-
return sb.toString();
123+
private String abbr(String s) {
124+
int n = s.length();
125+
return n < 3 ? s : s.charAt(0) + Integer.toString(n - 2) + s.charAt(n - 1);
136126
}
137127
}
138128

@@ -143,6 +133,81 @@ class ValidWordAbbr {
143133
*/
144134
```
145135

136+
### **C++**
137+
138+
```cpp
139+
class ValidWordAbbr {
140+
public:
141+
unordered_map<string, unordered_set<string>> words;
142+
143+
ValidWordAbbr(vector<string>& dictionary) {
144+
for (auto word : dictionary)
145+
{
146+
auto abbr = wordAbbr(word);
147+
words[abbr].insert(word);
148+
}
149+
}
150+
151+
bool isUnique(string word) {
152+
auto abbr = wordAbbr(word);
153+
if (!words.count(abbr)) return true;
154+
auto vals = words[abbr];
155+
return vals.size() == 1 && vals.count(word);
156+
}
157+
158+
string wordAbbr(string s) {
159+
int n = s.size();
160+
return n < 3 ? s : s.substr(0, 1) + to_string(n - 2) + s.substr(n - 1, 1);
161+
}
162+
};
163+
164+
/**
165+
* Your ValidWordAbbr object will be instantiated and called as such:
166+
* ValidWordAbbr* obj = new ValidWordAbbr(dictionary);
167+
* bool param_1 = obj->isUnique(word);
168+
*/
169+
```
170+
171+
### **Go**
172+
173+
```go
174+
type ValidWordAbbr struct {
175+
words map[string]map[string]bool
176+
}
177+
178+
func Constructor(dictionary []string) ValidWordAbbr {
179+
words := make(map[string]map[string]bool)
180+
for _, word := range dictionary {
181+
abbr := wordAbbr(word)
182+
if words[abbr] == nil {
183+
words[abbr] = make(map[string]bool)
184+
}
185+
words[abbr][word] = true
186+
}
187+
return ValidWordAbbr{words}
188+
}
189+
190+
func (this *ValidWordAbbr) IsUnique(word string) bool {
191+
abbr := wordAbbr(word)
192+
words := this.words[abbr]
193+
return words == nil || (len(words) == 1 && words[word])
194+
}
195+
196+
func wordAbbr(s string) string {
197+
n := len(s)
198+
if n <= 2 {
199+
return s
200+
}
201+
return s[0:1] + strconv.Itoa(n-2) + s[n-1:]
202+
}
203+
204+
/**
205+
* Your ValidWordAbbr object will be instantiated and called as such:
206+
* obj := Constructor(dictionary);
207+
* param_1 := obj.IsUnique(word);
208+
*/
209+
```
210+
146211
### **...**
147212

148213
```

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

Lines changed: 89 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,18 @@ validWordAbbr.isUnique(&quot;cake&quot;); // return true, because &quot;cake&quo
6969
class ValidWordAbbr:
7070

7171
def __init__(self, dictionary: List[str]):
72-
self.words = {}
72+
self.words = defaultdict(set)
7373
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
74+
abbr = self.word_abbr(word)
75+
self.words[abbr].add(word)
7876

7977
def isUnique(self, word: str) -> bool:
80-
abbr = self._word_abbr(word)
81-
vals = self.words.get(abbr)
82-
return vals isNoneor (len(vals) == 1 and word in vals)
78+
abbr = self.word_abbr(word)
79+
words = self.words[abbr]
80+
return not words or (len(words) == 1 and word in words)
8381

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]}'
82+
def word_abbr(self, s):
83+
return s if len(s) < 3 else f'{s[0]}{len(s) - 2}{s[-1]}'
8984

9085

9186
# Your ValidWordAbbr object will be instantiated and called as such:
@@ -102,25 +97,20 @@ class ValidWordAbbr {
10297
public ValidWordAbbr(String[] dictionary) {
10398
words = new HashMap<>();
10499
for (String word : dictionary) {
105-
String abbr = wordAbbr(word);
100+
String abbr = abbr(word);
106101
words.computeIfAbsent(abbr, k -> new HashSet<>()).add(word);
107102
}
108103
}
109-
104+
110105
public boolean isUnique(String word) {
111-
String abbr = wordAbbr(word);
106+
String abbr = abbr(word);
112107
Set<String> vals = words.get(abbr);
113108
return vals == null || (vals.size() == 1 && vals.contains(word));
114109
}
115110

116-
private String wordAbbr(String word) {
117-
int n = word.length();
118-
if (n < 3) {
119-
return word;
120-
}
121-
StringBuilder sb = new StringBuilder();
122-
sb.append(word.charAt(0)).append(n - 2).append(word.charAt(n - 1));
123-
return sb.toString();
111+
private String abbr(String s) {
112+
int n = s.length();
113+
return n < 3 ? s : s.charAt(0) + Integer.toString(n - 2) + s.charAt(n - 1);
124114
}
125115
}
126116

@@ -131,6 +121,81 @@ class ValidWordAbbr {
131121
*/
132122
```
133123

124+
### **C++**
125+
126+
```cpp
127+
class ValidWordAbbr {
128+
public:
129+
unordered_map<string, unordered_set<string>> words;
130+
131+
ValidWordAbbr(vector<string>& dictionary) {
132+
for (auto word : dictionary)
133+
{
134+
auto abbr = wordAbbr(word);
135+
words[abbr].insert(word);
136+
}
137+
}
138+
139+
bool isUnique(string word) {
140+
auto abbr = wordAbbr(word);
141+
if (!words.count(abbr)) return true;
142+
auto vals = words[abbr];
143+
return vals.size() == 1 && vals.count(word);
144+
}
145+
146+
string wordAbbr(string s) {
147+
int n = s.size();
148+
return n < 3 ? s : s.substr(0, 1) + to_string(n - 2) + s.substr(n - 1, 1);
149+
}
150+
};
151+
152+
/**
153+
* Your ValidWordAbbr object will be instantiated and called as such:
154+
* ValidWordAbbr* obj = new ValidWordAbbr(dictionary);
155+
* bool param_1 = obj->isUnique(word);
156+
*/
157+
```
158+
159+
### **Go**
160+
161+
```go
162+
type ValidWordAbbr struct {
163+
words map[string]map[string]bool
164+
}
165+
166+
func Constructor(dictionary []string) ValidWordAbbr {
167+
words := make(map[string]map[string]bool)
168+
for _, word := range dictionary {
169+
abbr := wordAbbr(word)
170+
if words[abbr] == nil {
171+
words[abbr] = make(map[string]bool)
172+
}
173+
words[abbr][word] = true
174+
}
175+
return ValidWordAbbr{words}
176+
}
177+
178+
func (this *ValidWordAbbr) IsUnique(word string) bool {
179+
abbr := wordAbbr(word)
180+
words := this.words[abbr]
181+
return words == nil || (len(words) == 1 && words[word])
182+
}
183+
184+
func wordAbbr(s string) string {
185+
n := len(s)
186+
if n <= 2 {
187+
return s
188+
}
189+
return s[0:1] + strconv.Itoa(n-2) + s[n-1:]
190+
}
191+
192+
/**
193+
* Your ValidWordAbbr object will be instantiated and called as such:
194+
* obj := Constructor(dictionary);
195+
* param_1 := obj.IsUnique(word);
196+
*/
197+
```
198+
134199
### **...**
135200

136201
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class ValidWordAbbr {
2+
public:
3+
unordered_map<string, unordered_set<string>> words;
4+
5+
ValidWordAbbr(vector<string>& dictionary) {
6+
for (auto word : dictionary)
7+
{
8+
auto abbr = wordAbbr(word);
9+
words[abbr].insert(word);
10+
}
11+
}
12+
13+
bool isUnique(string word) {
14+
auto abbr = wordAbbr(word);
15+
if (!words.count(abbr)) return true;
16+
auto vals = words[abbr];
17+
return vals.size() == 1 && vals.count(word);
18+
}
19+
20+
string wordAbbr(string s) {
21+
int n = s.size();
22+
return n < 3 ? s : s.substr(0, 1) + to_string(n - 2) + s.substr(n - 1, 1);
23+
}
24+
};
25+
26+
/**
27+
* Your ValidWordAbbr object will be instantiated and called as such:
28+
* ValidWordAbbr* obj = new ValidWordAbbr(dictionary);
29+
* bool param_1 = obj->isUnique(word);
30+
*/
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
type ValidWordAbbr struct {
2+
words map[string]map[string]bool
3+
}
4+
5+
func Constructor(dictionary []string) ValidWordAbbr {
6+
words := make(map[string]map[string]bool)
7+
for _, word := range dictionary {
8+
abbr := wordAbbr(word)
9+
if words[abbr] == nil {
10+
words[abbr] = make(map[string]bool)
11+
}
12+
words[abbr][word] = true
13+
}
14+
return ValidWordAbbr{words}
15+
}
16+
17+
func (this *ValidWordAbbr) IsUnique(word string) bool {
18+
abbr := wordAbbr(word)
19+
words := this.words[abbr]
20+
return words == nil || (len(words) == 1 && words[word])
21+
}
22+
23+
func wordAbbr(s string) string {
24+
n := len(s)
25+
if n <= 2 {
26+
return s
27+
}
28+
return s[0:1] + strconv.Itoa(n-2) + s[n-1:]
29+
}
30+
31+
/**
32+
* Your ValidWordAbbr object will be instantiated and called as such:
33+
* obj := Constructor(dictionary);
34+
* param_1 := obj.IsUnique(word);
35+
*/

‎solution/0200-0299/0288.Unique Word Abbreviation/Solution.java‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,20 @@ class ValidWordAbbr {
44
public ValidWordAbbr(String[] dictionary) {
55
words = new HashMap<>();
66
for (String word : dictionary) {
7-
String abbr = wordAbbr(word);
7+
String abbr = abbr(word);
88
words.computeIfAbsent(abbr, k -> new HashSet<>()).add(word);
99
}
1010
}
1111

1212
public boolean isUnique(String word) {
13-
String abbr = wordAbbr(word);
13+
String abbr = abbr(word);
1414
Set<String> vals = words.get(abbr);
1515
return vals == null || (vals.size() == 1 && vals.contains(word));
1616
}
1717

18-
private String wordAbbr(String word) {
19-
int n = word.length();
20-
if (n < 3) {
21-
return word;
22-
}
23-
StringBuilder sb = new StringBuilder();
24-
sb.append(word.charAt(0)).append(n - 2).append(word.charAt(n - 1));
25-
return sb.toString();
18+
private String abbr(String s) {
19+
int n = s.length();
20+
return n < 3 ? s : s.charAt(0) + Integer.toString(n - 2) + s.charAt(n - 1);
2621
}
2722
}
2823

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /