[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制作安装包(setup.py)
1.制作setup.py from distutils.core import setup setup(name='Myblog', version='1.0', description='My Bl ...
- [转]python 之字典{}(Hashmap)
字典 python里的字典就像java里的HashMap,以键值对的方式存在并操作,其特点如下 通过键来存取,而非偏移量: 键值对是无序的: 键和值可以是任意对象: 长度可变,任意嵌套: 在字典里,不 ...
- 在树莓派上部署asp.net
今天成功的在树莓派上部署asp.net呢.之前在unbuntu上测试成功了,结果今天操作的时候又不会操作了,主要对Linux太不熟悉了,找资料,资料又不多,这次赶紧记录下来,以备下次查阅. 我用的mo ...
- vi 在行首尾添加字符串
在行首尾添加字符串 用下列命令在文本每行的行首添加字符串“NewString”,请注意空格的存在. :g/^/s//NewString/g :%s/^/NewString/g ...
- 使用Memcached、Spring AOP构建数据库前端缓存框架
数据库访问可能是很多网站的瓶颈.动不动就连接池耗尽.内存溢出等.前面已经讲到如果我们的网站是一个分布式的大型站点,那么使用 memcached实现数据库的前端缓存是个很不错的选择:但如果网站本身足够小 ...
- 视图--bai
/*视图的必要性 create view population_all_view as select xxxx 详细信息 from qgck where rownum<500 -- sql语句不 ...
- 在asp.net中如何自己编写highcharts图表导出到自己的服务器上来
1.准备工作:网上下载highcharts导出的关键dll. 1).Svg.dll:因为highcharts的格式其实就是一个xml,采用svg的方式画图: 2).itextsha ...
- ZOJ 3713 In 7-bit
点我看题目 题意 : 这个题的英文叙述真的是太强了,真不知道哪里来的英文,完全看不懂,看了两个小时没弄懂真正的题意.就是给你一个字符串,先输出长度,但是长度要用二进制表示出来,二进制的低7位左边如果没 ...
- linux下查看机器的硬件信息:
查看CPU信息(型号) # cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c 8 Intel(R) Xeon(R) CPU ...
- USB Type-C 应用面临安全性考验,USB-IF 将推动新认证机制
USB 应用已经达到空前盛况,横跨电脑.移动设备.周边设备.影音器材等范畴,是一个极为普遍常见的界面.进入 USB Type-C 世代由于一并推动 USB-PD,过去没有严格执行的认证要求,基于安全性 ...