@@ -69,23 +69,18 @@ validWordAbbr.isUnique("cake"); // return true, because "cake&quo
69
69
class ValidWordAbbr :
70
70
71
71
def __init__ (self , dictionary : List[str ]):
72
- self .words = {}
72
+ self .words = defaultdict( set )
73
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
74
+ abbr = self .word_abbr(word)
75
+ self .words[abbr].add(word)
78
76
79
77
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 )
78
+ abbr = self .word_abbr (word)
79
+ words = self .words[ abbr]
80
+ return not words or (len (words ) == 1 and word in words )
83
81
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 ]} '
89
84
90
85
91
86
# Your ValidWordAbbr object will be instantiated and called as such:
@@ -102,25 +97,20 @@ class ValidWordAbbr {
102
97
public ValidWordAbbr (String [] dictionary ) {
103
98
words = new HashMap<> ();
104
99
for (String word : dictionary) {
105
- String abbr = wordAbbr (word);
100
+ String abbr = abbr (word);
106
101
words. computeIfAbsent(abbr, k - > new HashSet<> ()). add(word);
107
102
}
108
103
}
109
-
104
+
110
105
public boolean isUnique (String word ) {
111
- String abbr = wordAbbr (word);
106
+ String abbr = abbr (word);
112
107
Set<String > vals = words. get(abbr);
113
108
return vals == null || (vals. size() == 1 && vals. contains(word));
114
109
}
115
110
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 );
124
114
}
125
115
}
126
116
@@ -131,6 +121,81 @@ class ValidWordAbbr {
131
121
*/
132
122
```
133
123
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
+
134
199
### ** ...**
135
200
136
201
```
0 commit comments