[Locked] Unique Word Abbreviation
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
所以当缩写存在时,也并非一定要return false,如果原字典中与query缩写一致的原字符串,如2中dict的两个"door",与query原字符串"door"一致,那么也应该return true。
代码:
class Solution {
private:
string maptoabbr(string str) {
string abbr = "";
abbr += str[];
//若只有一位,则直接返回;有两位,则中间不加数字;两个以上,则加数字
if(str.length() > ) {
abbr += str.length() > ? to_string(str.length() - ) : "";
abbr += str.back();
}
return abbr;
}
//hashabbr用来存储缩写后的字符串,hashorig用来存储原始字符串
unordered_multiset<string> hashabbr, hashorig; public:
Solution(vector<string> dict) {
for(string str : dict) {
hashorig.insert(str);
hashabbr.insert(maptoabbr(str));
}
}
bool isUnique(string str) {
string abbr = maptoabbr(str);
//如果缩写不存在字典中,直接return true
if(hashabbr.find(abbr) == hashabbr.end())
return true;
//如果缩写在字典中,则如果query只对应一种原始字符串,则return true;否则return false
return hashabbr.count(abbr) == hashorig.count(str);
}
};
[Locked] Unique Word Abbreviation的更多相关文章
- [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
A string such as "word" contains the following abbreviations: ["word", "1or ...
- Leetcode 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> ...
- Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [LeetCode] Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [Swift]LeetCode288. 唯一单词缩写 $ Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- Unique Word Abbreviation -- LeetCode
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [LeetCode] 288.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 ...
随机推荐
- python模块之os和os.path模块
1.os模块os.listdir(dirname) 列出dirname下的目录和文件os.getcwd()函数得到当前工作目录,即当前Python脚本工作的目录路径.os.getenv()和os.pu ...
- RS232转RS485电路图分析
在电子发烧友网站上,看到RS232转RS485的一个电路图,如下图所示.元件主要是HN232CP和MAX485CPA,也就是TTL转232电路和TTL转485电路的结合体.可是这个电路却不好分析,几经 ...
- 计算S(n)=a+aa+aaa+...... 其中a是一个数字
描述 计算S(n)=a+aa+aaa+...... 其中a是一个数字 输入数据 两个分别表示a和n的整数 输出数据 一个表示S(n)的整数 输入示例 3 5 输出示例 37035 # include ...
- 转 JavaScript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)
收藏一下 1.判断select选项中 是否存在Value=”paraValue”的Item2.向select选项中 加入一个Item3.从select选项中 删除一个Item4.删除select中选中 ...
- Linux系统架设支持自助开通Shado wsocks及VPN前端的教程
程序实现:通过网页端注册,自助开通VPN帐号及Shadowsocks帐号.并可实现流量统计 系统要求 Debian 6 x64 纯净系统 by: Lop ①配置环境 apt-get updateapt ...
- delphi xe5 android 使用样式(风格)
1.在界面上添加 TStyleBook 控件 2.点击Resource 选择xe5程序安装带的几个风格的其中之一,路径存放在: C:\Program Files\Embarcadero\RAD Stu ...
- php smarty 缓存和配置文件的基本使用方法
smarty高级部分包括缓存机制和配置文件的调用 下面是代码实现: 文件一,配置文件: #全局变量 title="网站主页" content="一个网站的主体部分&quo ...
- spring+mybatis 框架搭建
注意<!-- 中间的字要保持与左右留出一个空格,否则会报错说出现两杠线 --> 1.导入jar包 aopalliance-1.0.jarasm-3.3.1.jarcglib-2.2.2.j ...
- 学习kernel编程的建议
我把我学习kernel编程的过程介绍给大家,希望大家有个参考. 学习kernel编程需要阅读大量的kernel方面的书籍,在此我列举一下我读过的kernel书籍(按时间先后顺序),并给一些建议. 1. ...
- 【HDOJ】3311 Dig The Wells
Steiner Tree.概念就不讲了,引入0号结点.[1, n+m]到0连一条边,权重表示挖井的费用.这样建图spfa求MST即满足所求解. /* 3311 */ #include <iost ...