An abbreviation of a word follows the form <first letter><number><last letter>. Below are some examples of word abbreviations:

a) it                      --> it    (no abbreviation)

     1
b) d|o|g --> d1g 1 1 1
1---5----0----5--8
c) i|nternationalizatio|n --> i18n 1
1---5----0
d) l|ocalizatio|n --> l10n

Assume you have a dictionary and given a word, find whether its abbreviation is unique in the dictionary. A word's abbreviation is unique if no other word from the dictionary has the same abbreviation.

Example:

Given dictionary = [ "deer", "door", "cake", "card" ]

isUnique("dear") -> false
isUnique("cart") -> true
isUnique("cane") -> false
isUnique("make") -> true
Show Company Tags
Show Tags
Show Similar Problems
 
 class ValidWordAbbr {
private:
unordered_map<string, int> dict;
unordered_set<string> inDict;
string getAbbr(string word) {
if (word.size() < ) return word;
return word.front() + std::to_string(word.size() - ) + word.back();
}
public:
ValidWordAbbr(vector<string> &dictionary) {
for (int i = , n = dictionary.size(); i < n; i++) {
if (inDict.count(dictionary[i])) continue;
inDict.insert(dictionary[i]);
string abbr = getAbbr(dictionary[i]);
if (dict.count(abbr) == )
dict.insert(make_pair(abbr, ));
else dict[abbr]++;
}
} bool isUnique(string word) {
string abbr = getAbbr(word);
if (dict.count(abbr) == || (dict[abbr] == && inDict.count(word) == ))
return true;
return false;
}
}; // Your ValidWordAbbr object will be instantiated and called as such:
// ValidWordAbbr vwa(dictionary);
// vwa.isUnique("hello");
// vwa.isUnique("anotherWord");

要点:int转string可以用函数std::to_string()

inDict 的作用有两个:1)将所给的字典去重。 2)判断要检测的单词是否在所给的字典中。

Unique Word Abbreviation -- LeetCode的更多相关文章

  1. [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写

    A string such as "word" contains the following abbreviations: ["word", "1or ...

  2. [Locked] Unique Word Abbreviation

    Unique Word Abbreviation An abbreviation of a word follows the form <first letter><number&g ...

  3. Leetcode Unique Word Abbreviation

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  4. 288. Unique Word Abbreviation

    题目: An abbreviation of a word follows the form <first letter><number><last letter> ...

  5. Unique Word Abbreviation

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  6. [LeetCode] Unique Word Abbreviation 独特的单词缩写

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  7. [LeetCode] 288.Unique Word Abbreviation 独特的单词缩写

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  8. Leetcode: Minimum Unique Word Abbreviation

    A string such as "word" contains the following abbreviations: ["word", "1or ...

  9. [Swift]LeetCode288. 唯一单词缩写 $ Unique Word Abbreviation

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

随机推荐

  1. 遍历两个自定义列表来实现字典(key:value)

    #自定义key    ${keys}    create list    key1    key2    key3 #自定义value    ${values}    create list    v ...

  2. maven常用命令 与语法

    pom.xml 中个元素的意义 groupId 规定了这个项目属于哪个组,或者公司之类的 artifactId 定义了当前maven项目在组中唯一的ID version 版本号 常用命令 mvn co ...

  3. (转\整)UE4游戏优化 多人大地型游戏的优化(二)渲染线程的优化

    施主分享随缘,评论随心,@author:白袍小道 小道暗语: 1.因为小道这里博客目录没自己整,暂时就用随笔目录结构,所以二级目录那啥就忽略了.标题格式大致都是(原or转) 二级目录 (标题) 2.因 ...

  4. 【志银】MySQL命令总结

    ===0-MySQL密码设置===0.1-登入和进入MySQL数据库: 0.1.1-登入MySQL数据库:C:\Users\Administrator>mysql -u用户名 -hMySQL服务 ...

  5. ssh 免交互登录 ,远程执行命令脚本。

    ##免交互SSH登录auto_login_ssh () {    expect -c "set timeout -1;                spawn -noecho ssh -o ...

  6. A Neural Algorithm of Artistic Style

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/53931536 1. 资源 Paper: ...

  7. MyBatis中使用OGNL表达式与操作符

    String与基本数据类型 _parameter 自定义类型(对象) 属性名 集合 数组 array List list Map _parameter 获取集合中的一条数据 数组 array[索引] ...

  8. BZOJ 2653 middle | 主席树

    题目: http://www.lydsy.com/JudgeOnline/problem.php?id=2653 题解: 设答案为ans,把大于等于ans的记为1,小于的记为-1,这样可以知道当前an ...

  9. 刷题总结——Throw nails(hdu4393)

    题目: Problem Description The annual school bicycle contest started. ZL is a student in this school. H ...

  10. [1]区分event对象中的[clientX,offsetX,screenX,pageX]

    前言 在平时的开发中,非常讨厌的就是兼容性了,兼容性的问题总会让我们记忆混淆,所以这次来区分一下event对象中的常用获取鼠标位置. clientX clientY event.clientXeven ...