nikunjsoni

288

Jun 22nd, 2021
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. class ValidWordAbbr {
  2. public:
  3.     ValidWordAbbr(vector<string> &dictionary) {
  4.         for (string& d : dictionary) {
  5.             int n = d.length();
  6.             string abbr = d[0] + to_string(n) + d[n - 1];
  7.             mp[abbr].insert(d);
  8.         }
  9.     }
  10.  
  11.     bool isUnique(string word) {
  12.         int n = word.length();
  13.         string abbr = word[0] + to_string(n) + word[n - 1];
  14.         return mp[abbr].count(word) == mp[abbr].size();
  15.     }
  16. private:
  17.     unordered_map<string, unordered_set<string>> mp;
  18. };
  19.  
  20.  
  21. /**
  22.  * Your ValidWordAbbr object will be instantiated and called as such:
  23.  * ValidWordAbbr* obj = new ValidWordAbbr(dictionary);
  24.  * bool param_1 = obj->isUnique(word);
  25.  */
Advertisement
Add Comment
Please, Sign In to add comment