Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ValidWordAbbr {
- public:
- unordered_map < string , int > abb;
- ValidWordAbbr(vector<string>& dictionary) {
- for (int i = 0 ; i < dictionary.size(); i ++ )
- {
- string cur_word = dictionary[i];
- string cur_abb;
- if (abb[cur_word])
- continue;
- abb[cur_word]++;
- if (cur_word.size() > 2)
- {
- string number = to_string (cur_word.size() - 2);
- cur_abb = cur_word[0] + number + cur_word[cur_word.size() - 1];
- abb[cur_abb] ++;
- }
- }
- }
- bool isUnique(string cur_word) {
- string cur_abb;
- if (cur_word.size() < 3)
- cur_abb = cur_word;
- else
- {
- string number = to_string (cur_word.size() - 2);
- cur_abb = cur_word[0] + number + cur_word[cur_word.size() - 1];
- }
- return abb[cur_abb] == 0 || (abb[cur_abb] == 1 && abb[cur_word] == 1);
- }
- };
- /**
- * Your ValidWordAbbr object will be instantiated and called as such:
- * ValidWordAbbr* obj = new ValidWordAbbr(dictionary);
- * bool param_1 = obj->isUnique(word);
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement