[LeetCode] Unique Word Abbreviation 独特的单词缩写
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
参考资料:
https://leetcode.com/problems/unique-word-abbreviation/
https://leetcode.com/problems/unique-word-abbreviation/discuss/73133/8-lines-in-C%2B%2B...
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Unique Word Abbreviation 独特的单词缩写的更多相关文章
- [LeetCode] 288.Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- 408. Valid Word Abbreviation有效的单词缩写
[抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...
- Leetcode Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
A string such as "word" contains the following abbreviations: ["word", "1or ...
- [LeetCode] Valid Word Abbreviation 验证单词缩写
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- [LeetCode] 527. Word Abbreviation 单词缩写
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
- [Locked] Unique Word Abbreviation
Unique Word Abbreviation An abbreviation of a word follows the form <first letter><number&g ...
- [Swift]LeetCode288. 唯一单词缩写 $ Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- 288. Unique Word Abbreviation
题目: An abbreviation of a word follows the form <first letter><number><last letter> ...
随机推荐
- 用CIL写程序:写个函数做加法
前言: 上一篇文章小匹夫为CIL正名的篇幅比较多,反而忽略了写那篇文章初衷--即通过写CIL代码来熟悉它,了解它.那么既然有上一篇文章做基础(炮灰),想必各位对CIL的存在也就释然了,兴许也燃起了一点 ...
- 编写高质量代码:改善Java程序的151个建议(第4章:字符串___建议56~59)
建议56:自由选择字符串拼接方法 对一个字符串拼接有三种方法:加号.concat方法及StringBuilder(或StringBuffer ,由于StringBuffer的方法与StringBuil ...
- unsafe
今天无意中发现C#这种完全面向对象的高级语言中也可以用不安全的指针类型,即要用到unsafe关键字.在公共语言运行库 (CLR) 中,不安全代码是指无法验证的代码.C# 中的不安全代码不一定是危险的, ...
- AC自动机-算法详解
What's Aho-Corasick automaton? 一种多模式串匹配算法,该算法在1975年产生于贝尔实验室,是著名的多模式匹配算法之一. 简单的说,KMP用来在一篇文章中匹配一个模式串:但 ...
- 福利到!Rafy(原OEA)领域实体框架 2.22.2067 发布!
距离“上次框架完整发布”已经过去了一年半了,应群中的朋友要求,决定在国庆放假之际,把最新的框架发布出来,并把帮助文档整理出来,这样可以方便大家快速上手. 发布内容 注意,本次发布,只包含 Rafy ...
- Markdown通用的常用语法说明
前言 Markdown 是一种轻量级的 标记语言,语法简洁明了.学习容易,还具有其他很多优点,目前被越来越多的人用来写作使用. Markdown具有一系列衍生版本,用于扩展Markdown的功能(如表 ...
- "bower.json 中出现语法错误" 的解决方案之一
当你用 Visual Studio 2015 Update 3 打开从别处下载的开源项目的时候,如果发现 Bower 提示 "bower.json 中出现语法错误". 请检查一下. ...
- StackExchange.Redis帮助类解决方案RedisRepository封装(散列Hash类型数据操作)
本文版权归博客园和作者本人共同所有,转载和爬虫请注明本系列分享地址:http://www.cnblogs.com/tdws/p/5815735.html 上一篇文章的不合理之处,已经有所修改. 今天分 ...
- struts2学习之旅三 权限管理和导航设计
1,权限管理的db设计和dao实现,尽量简单快速有效: db的设计如下:权限按照角色来赋给用户: 权限对应每一个具体的功能,有菜单级别的,有导航级别的,还有页面级别的功能: 涉及到权限的敏感操作一般都 ...
- nginx反向代理下thinkphp、php获取不到正确的外网ip
在记录用户发送短信需要获取用户ip时,tp一直获取的是内网ip:10.10.10.10 tp框架获取ip方法:get_client_ip /** * 获取客户端IP地址 * @param intege ...