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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -59,25 +59,33 @@ validWordAbbr.isUnique("cake"); // return true, because "cake&quo
## Solutions
**Solution 1: Hash Table**
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.
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)]$.
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.
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)$.
<!-- tabs:start -->
### **Python3**
```python
class ValidWordAbbr:
def __init__(self, dictionary: List[str]):
self.words = defaultdict(set)
for word in dictionary:
abbr = self.word_abbr(word)
self.words[abbr].add(word)
self.d = defaultdict(set)
for s in dictionary:
self.d[self.abbr(s)].add(s)
def isUnique(self, word: str) -> bool:
abbr = self.word_abbr(word)
words = self.words[abbr]
return not words or (len(words) == 1 and word in words)
s = self.abbr(word)
return s not in self.d or all(word == t for t in self.d[s])
def word_abbr(self, s):
return s if len(s) < 3 else f'{s[0]}{len(s) - 2}{s[-1]}'
def abbr(self, s: str) -> str:
return s if len(s) < 3 else s[0] + str(len(s) - 2) + s[-1]
# Your ValidWordAbbr object will be instantiated and called as such:
Expand All
@@ -89,25 +97,22 @@ class ValidWordAbbr:
```java
class ValidWordAbbr {
private Map<String, Set<String>> words;
private Map<String, Set<String>> d = new HashMap<>();
public ValidWordAbbr(String[] dictionary) {
words = new HashMap<>();
for (String word : dictionary) {
String abbr = abbr(word);
words.computeIfAbsent(abbr, k -> new HashSet<>()).add(word);
for (var s : dictionary) {
d.computeIfAbsent(abbr(s), k -> new HashSet<>()).add(s);
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.