Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. class ValidWordAbbr {
  2. public:
  3. unordered_map < string , int > abb;
  4. ValidWordAbbr(vector<string>& dictionary) {
  5.  
  6. for (int i = 0 ; i < dictionary.size(); i ++ )
  7. {
  8. string cur_word = dictionary[i];
  9. string cur_abb;
  10.  
  11. if (abb[cur_word])
  12. continue;
  13.  
  14. abb[cur_word]++;
  15.  
  16. if (cur_word.size() > 2)
  17. {
  18. string number = to_string (cur_word.size() - 2);
  19. cur_abb = cur_word[0] + number + cur_word[cur_word.size() - 1];
  20.  
  21. abb[cur_abb] ++;
  22. }
  23.  
  24.  
  25.  
  26. }
  27. }
  28.  
  29. bool isUnique(string cur_word) {
  30.  
  31. string cur_abb;
  32. if (cur_word.size() < 3)
  33. cur_abb = cur_word;
  34. else
  35. {
  36. string number = to_string (cur_word.size() - 2);
  37. cur_abb = cur_word[0] + number + cur_word[cur_word.size() - 1];
  38. }
  39.  
  40. return abb[cur_abb] == 0 || (abb[cur_abb] == 1 && abb[cur_word] == 1);
  41.  
  42. }
  43. };
  44.  
  45. /**
  46. * Your ValidWordAbbr object will be instantiated and called as such:
  47. * ValidWordAbbr* obj = new ValidWordAbbr(dictionary);
  48. * bool param_1 = obj->isUnique(word);
  49. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement