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

这道题让我们求独特的单词缩写,但是题目中给的例子不是很清晰,来看下面三种情况:

1. dictionary = {"dear"},  isUnique("door") -> false

2. dictionary = {"door", "door"}, isUnique("door") -> true

3. dictionary = {"dear", "door"}, isUnique("door") -> false

从上面三个例子可以看出,当缩写一致的时候,字典中的单词均和给定单词相同时,返回 true。这里需要用 HashMap 来建立缩写形式和其对应的单词的映射,把所有缩写形式的相同单词放到一个 HashSet 中,然后再判断是否 unique 的时候只需要看给定单词的缩写形式的 HashSet 里面该单词的个数是否和 HashSet 中的元素总数相同,相同的话就是上面的第二种情况,返回 true。需要注意的是由于 HashSet 中不能有重复值,所有上面第二种情况只会有一个 door 存在 HashSet 里,但是并不影响判断结果,参见代码如下:

解法一:

class ValidWordAbbr {
public:
ValidWordAbbr(vector<string>& dictionary) {
for (auto a : dictionary) {
string k = a;
if (a.size() > ) k = a[] + to_string(a.size() - ) + a.back();
m[k].insert(a);
}
}
bool isUnique(string word) {
string k = word;
if (word.size() > ) k = word[] + to_string(word.size() - ) + word.back();
return m[k].count(word) == m[k].size();
} private:
unordered_map<string, unordered_set<string>> m;
};

如果我们想省一些空间,也可以不用 HashSet,但如何区分上面的第二和第三种情况呢,在遇到 HashMap 中没有当前缩写形式的时候,将该缩写形式和当前单词建立映射,如果该缩写形式应经存在,那么看如果映射的单词不是当前单词,将映射单词改为空字符串,这样做的原因是,在对于第三种情况 dictionary = {"dear", "door"} 时,遍历 dear 时,建立 d2r 和 dear 的映射,当遍历到 door 的时候,由于 door 和 dear 不同,将映射改为 d2r 和 "" 映射,而对于第二种情况 dictionary = {"door", "door"},保留 d2r 和 door 的映射,那么这样在判断 door 是否 unique 时,就可以区别第二种和第三种情况了,参见代码如下:

解法二:

class ValidWordAbbr {
public:
ValidWordAbbr(vector<string>& dictionary) {
for (auto a : dictionary) {
string k = a;
if (a.size() > ) k = a[] + to_string(a.size() - ) + a.back();
if (m.find(k) != m.end() && m[k] != a) m[k] = "";
else m[k] = a;
}
}
bool isUnique(string word) {
string k = word;
if (word.size() > ) k = word[] + to_string(word.size() - ) + word.back();
return m.find(k) == m.end() || m[k] == word;
}
private:
unordered_map<string, string> m;
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/288

类似题目:

Two Sum III - Data structure design

Generalized Abbreviation

参考资料:

https://leetcode.com/problems/unique-word-abbreviation/

https://leetcode.com/problems/unique-word-abbreviation/discuss/73133/8-lines-in-C%2B%2B...

https://leetcode.com/problems/unique-word-abbreviation/discuss/73143/Java-Solution-with-One-HashMaplessString-Stringgreater-beats-90-of-Submissions

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Unique Word Abbreviation 独特的单词缩写的更多相关文章

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

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

  2. 408. Valid Word Abbreviation有效的单词缩写

    [抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...

  3. Leetcode Unique Word Abbreviation

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

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

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

  5. [LeetCode] Valid Word Abbreviation 验证单词缩写

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  6. [LeetCode] 527. Word Abbreviation 单词缩写

    Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...

  7. [Locked] Unique Word Abbreviation

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

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

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

  9. 288. Unique Word Abbreviation

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

随机推荐

  1. SQL Server 统计信息更新时采样百分比对数据预估准确性的影响

    为什么要写统计信息 最近看到园子里有人写统计信息,楼主也来凑热闹. 话说经常做数据库的,尤其是做开发的或者优化的,统计信息造成的性能问题应该说是司空见惯. 当然解决办法也并非一成不变,“一招鲜吃遍天” ...

  2. 代码的坏味道(13)——过多的注释(Comments)

    坏味道--过多的注释(Comments) 特征 注释本身并不是坏事.但是常常有这样的情况:一段代码中出现长长的注释,而它之所以存在,是因为代码很糟糕. 问题原因 注释的作者意识到自己的代码不直观或不明 ...

  3. MessageBox.Show()的各种用法

    [函数] <整型> MessageBox(<字符串> Text, <字符串> Title, <整型> nType,MessageBoxIcon); [函 ...

  4. sql 修改字段默认值

    1.查出该字段的约束名称 SELECT c.name FROM sysconstraints a INNER JOIN syscolumns b on a.colid=b.colid INNER JO ...

  5. Eclipse复制时的中文乱码问题

    点击"Project"--"Properties"--"Resource",在其中改变"Text file encoding&qu ...

  6. java 开发模式

    Java-开发模式 Java Web开发方案有多种,这里列举一些经典的开发模式进行横向比较JSP+JAVABEAN开发模式:    特点:该模式将业务逻辑与页面表现进行分离,在一定程度上增加了程序的可 ...

  7. TabLayout+ViewPager+Fragment制作页卡

    本人很懒,直接上代码了. 布局文件: <?xml version="1.0" encoding="utf-8"?><android.suppo ...

  8. RecyclerView如何消除底部的分割线

    最近遇到一个问题,用RecyclerView显示数据,纵向列表显示,添加默认分割线.   问题是:底部也会显示分割线,这很影响美观.   怎么解决这个问题呢?我想了很多办法,毫无头绪...   最后, ...

  9. Android设置图片内存溢出(OOM)问题——Android开发进阶之路6

    ImageView设置图片必备常识技术: Android设备会给每个应用分配16M的内存空间,如果你设置的图片的比较大且同一个页面有多个时,经常会报OOM错误导致程序奔溃.所以在这种情况下我们必须要对 ...

  10. 安卓DJ113舞曲网应用客户端 项目源码(服务器+客户端)

    Android DJ113舞曲网app客户端 播放器源码  项目源码(服务器+客户端),这个项目整体有点类似天天动听的效果,非常漂亮的,支持第三方登录等功能,非常完整的一个音乐项目. 源码下载:htt ...