Leetcode 288 Solution
This article provides solution to leetcode question 288 (unique-word-abbreviation)
Access this page by simply typing in "lcs 288" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/unique-word-abbreviation
Solution
class ValidWordAbbr {
string getAbbr(const string& word)
{
if (word.size() <= 2)
return word;
string abbr = word.substr(0, 1);
abbr += to_string(word.size() - 2);
abbr += word[word.size() - 1];
return abbr;
}
unordered_map<string, int> s;
unordered_map<string, int> words;
public:
ValidWordAbbr(vector<string> dictionary) {
for (auto word : dictionary)
{
if (words[word] > 0)
continue;
words[word]++;
auto abbr = getAbbr(word);
s[abbr]++;
}
}
bool isUnique(string word) {
auto abbr = getAbbr(word);
return words[word] == 0 && s[abbr] == 0 || words[word] == 1 && s[abbr] == 1;
}
};
/**
* Your ValidWordAbbr object will be instantiated and called as such:
* ValidWordAbbr obj = new ValidWordAbbr(dictionary);
* bool param_1 = obj.isUnique(word);
*/