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. Linux认知之旅【05 进一步了解Linux装软件】!

    一.Linux软件管理系统 二.Linux还可以用源码安装 三.Linux软件配置

  2. ssh.sh_for_ubuntu1204

    #!/bin/bash sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/g' /etc/ssh/sshd_config s ...

  3. pytorch版本问题:AttributeError: 'module' object has no attribute '_rebuild_tensor_v2'

    用pytorch加载训练好的模型的时候遇到了如下的问题: AttributeError: 'module' object has no attribute '_rebuild_tensor_v2' 到 ...

  4. NodeJs01 文件浏览器

    ES6常用新语法 前言 是时候学点新的JS了! 为了在学习NodeJs之前,能及时用上语言的新特性,我们打算从一开始先学习一下JavaScript语言的最基本最常用新语法.本课程的内容,是已经假设你有 ...

  5. java课后作业2017.10.20

    动手动脑1: public class Test{ public static void main(String args[]) { Foo obj1=new Foo(); }}class Foo{ ...

  6. Android M中 JNI的入门学习

    今年谷歌推出了Android 6.0,作为安卓开发人员,对其学习掌握肯定是必不可少的,今天小编和大家分享的就是Android 6.0中的 JNI相关知识,这是在一个安卓教程网上看到的内容,感觉很不错, ...

  7. Password [分块]

    题面 $n,m,x \leq 10^5$ 思路 首先$n=2$做法很多,不讲了 $n=3$的时候,分块维护两个东西:每一个数出现次数的前缀和,和出现次数的出现次数的前缀和(说的有点绕,但是应该挺好理解 ...

  8. POJ 2195 Going Home | 带权二分图匹配

    给个地图有人和房子 保证人==房子,每个人移动到房子处需要花费曼哈顿距离的代价 问让人都住在房子里最小代价 显然是个带权二分图最大匹配 转化成以一个网络,规定w是容量,c是代价 1.S向人连边,w=1 ...

  9. 2-SAT学习整理

    关于2-SAT 问题给出的证明和思路就不再赘述 核心是对于问题给出的条件建图,然后跑tarjan缩点 (在一个强联通分量里bool值是相同的) 看集合两个元素是否在一个强联通分量来判断是否合法 利用强 ...

  10. JSP、JSTL、EF学习笔记

    JSP 1)Java Server Page,在html中嵌入java代码 2)九个内置(隐式)对象 request response out page pageContext config sess ...