You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0200-0299/0288.Unique Word Abbreviation/README_EN.md
+78-42
Original file line number
Diff line number
Diff line change
@@ -59,25 +59,33 @@ validWordAbbr.isUnique("cake"); // return true, because "cake&quo
59
59
60
60
## Solutions
61
61
62
+
**Solution 1: Hash Table**
63
+
64
+
According to the problem description, we define a function $abbr(s)$, which calculates the abbreviation of the word $s$. If the length of the word $s$ is less than $3$, then its abbreviation is itself; otherwise, its abbreviation is its first letter + (its length - 2) + its last letter.
65
+
66
+
Next, we define a hash table $d$, where the key is the abbreviation of the word, and the value is a set, the elements of which are all words abbreviated as that key. We traverse the given word dictionary, and for each word $s$ in the dictionary, we calculate its abbreviation $abbr(s)$, and add $s$ to $d[abbr(s)]$.
67
+
68
+
When judging whether the word $word$ meets the requirements of the problem, we calculate its abbreviation $abbr(word)$. If $abbr(word)$ is not in the hash table $d$, then $word$ meets the requirements of the problem; otherwise, we judge whether there is only one element in $d[abbr(word)]$. If there is only one element in $d[abbr(word)]$ and that element is $word$, then $word$ meets the requirements of the problem.
69
+
70
+
In terms of time complexity, the time complexity of initializing the hash table is $O(n)$, where $n$ is the length of the word dictionary; the time complexity of judging whether a word meets the requirements of the problem is $O(1)$. In terms of space complexity, the space complexity of the hash table is $O(n)$.
71
+
62
72
<!-- tabs:start -->
63
73
64
74
### **Python3**
65
75
66
76
```python
67
77
classValidWordAbbr:
68
78
def__init__(self, dictionary: List[str]):
69
-
self.words = defaultdict(set)
70
-
for word in dictionary:
71
-
abbr =self.word_abbr(word)
72
-
self.words[abbr].add(word)
79
+
self.d = defaultdict(set)
80
+
for s in dictionary:
81
+
self.d[self.abbr(s)].add(s)
73
82
74
83
defisUnique(self, word: str) -> bool:
75
-
abbr =self.word_abbr(word)
76
-
words =self.words[abbr]
77
-
returnnot words or (len(words) ==1and word in words)
84
+
s =self.abbr(word)
85
+
return s notinself.d orall(word == t for t inself.d[s])
78
86
79
-
defword_abbr(self, s):
80
-
return s iflen(s) <3elsef'{s[0]}{len(s) -2}{s[-1]}'
87
+
defabbr(self, s: str) -> str:
88
+
return s iflen(s) <3else s[0]+str(len(s) -2) +s[-1]
81
89
82
90
83
91
# Your ValidWordAbbr object will be instantiated and called as such:
@@ -89,25 +97,22 @@ class ValidWordAbbr:
89
97
90
98
```java
91
99
classValidWordAbbr {
92
-
privateMap<String, Set<String>>words;
100
+
privateMap<String, Set<String>>d =newHashMap<>();
93
101
94
102
publicValidWordAbbr(String[] dictionary) {
95
-
words =newHashMap<>();
96
-
for (String word : dictionary) {
97
-
String abbr = abbr(word);
98
-
words.computeIfAbsent(abbr, k ->newHashSet<>()).add(word);
103
+
for (var s : dictionary) {
104
+
d.computeIfAbsent(abbr(s), k ->newHashSet<>()).add(s);
0 commit comments