#include <string>
#include <vector>
#include <unordered_set>
#include <iostream>
using namespace std;
/*
An abbreviation of a word follows the form <first letter><number><last letter>
Below are some example of word abbreviations.
1: it -> it
2: dog -> d1g
3: internationalization -> i18n
*/
// just use an unordered_set is enough.....
class ValidWordAbbr{
private:
unordered_set<string> map;
public:
ValidWordAbbr(vector<string>& dictionary) {
for(int i = 0; i < dictionary.size(); ++i) {
int n = dictionary[i].size();
if(n <= 2) map.emplace(dictionary[i]);
else {
string tmp = dictionary[i][0] + to_string(n - 2) + dictionary[i][n-1];
map.emplace(tmp);
}
}
}
bool isUnique(string word) {
int n = word.size();
if(n > 2) {
string tmp = word[0] + to_string(n-2) + word[n-1];
} else tmp = word;
if(map.find(tmp) == map.end()) return true;
else return false;
}
};
int main(void) {
vector<string> dictionary{"deer", "door", "cake", "card"};
ValidWordAbbr strMap(dictionary);
cout << strMap.isUnique("datr") << endl;
}
LeetCode 256. Unique Word Abbreviation
最新推荐文章于 2024-04-17 22:32:42 发布