[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> ...
随机推荐
- 国内maven镜像,快的飞起
在oschina关来关去的烦恼下,终于受不了去寻找其他公共库了. 阿里云maven镜像 <mirrors> <mirror> <id>alimaven</id ...
- ASP.NET Core 中文文档 第二章 指南(4.2)添加 Controller
原文:Adding a controller 翻译:娄宇(Lyrics) 校对:刘怡(AlexLEWIS).何镇汐.夏申斌.孟帅洋(书缘) Model-View-Controller (MVC) 架构 ...
- JavaWeb——tomcat安装及目录介绍
一.web web可以说,就是一套 请求->处理->响应 的流程.客户端使用浏览器(IE.FireFox等),通过网络(Network)连接到服务器上,使用HTTP协议发起请求(Reque ...
- Docker容器环境下ASP.NET Core Web API应用程序的调试
本文主要介绍通过Visual Studio 2015 Tools for Docker – Preview插件,在Docker容器环境下,对ASP.NET Core Web API应用程序进行调试.在 ...
- Python黑帽编程 3.4 跨越VLAN
Python黑帽编程 3.4 跨域VLAN VLAN(Virtual Local Area Network),是基于以太网交互技术构建的虚拟网络,既可以将同一物理网络划分成多个VALN,也可以跨越物理 ...
- Linux用户体系和文件权限总结
一. Linux系统用户和用户组相关文件 1. /etc/passwd文件 这个passwd文件是Linux用户信息文件.文件格式说明如下: root:x:0:0:root:/r ...
- 创建或打开解决方案时提示"DotNetCore.1.0.1-SDK.1.0.0.Preview2-003131-x86"错误的解决方案
提示"DotNetCore.1.0.1-SDK.1.0.0.Preview2-003131-x86"错误的解决方案: 1.检查是否有C:\Program Files (x86)\d ...
- Entity Framework 教程——什么是Entity Framework
什么是Entity Framework 编写和管理ADO.NET是一个繁琐而又无聊的工作.微软为你的应用提供了一个名为"Entity Framework"的ORM框架来自动化管理你 ...
- 【译】Asp.net mvc 使用ITextSharp PDF to HTML (解决img标签问题)
前言:因项目需求,需要将HTML代码转成PDF.大致上已经实现了,可以是发现使用ITextSharp(我现在的版本是5.5.9)的时候,img标签中的src只能跟绝对路径. 在百度上找了一个上午,有一 ...
- fluent批量处理——模型参数的设置
对于常见的工程应用来说,计算的工况很多,尤其优化工作,少则几百,多则上千,面对如此之多的case文件要写,假如按照一个一个的读写的话,相信你一定会为这么机械的工作烦躁,甚至影响今后好几天的心情,那么有 ...