Unique Word Abbreviation -- LeetCode
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
class ValidWordAbbr {
private:
unordered_map<string, int> dict;
unordered_set<string> inDict;
string getAbbr(string word) {
if (word.size() < ) return word;
return word.front() + std::to_string(word.size() - ) + word.back();
}
public:
ValidWordAbbr(vector<string> &dictionary) {
for (int i = , n = dictionary.size(); i < n; i++) {
if (inDict.count(dictionary[i])) continue;
inDict.insert(dictionary[i]);
string abbr = getAbbr(dictionary[i]);
if (dict.count(abbr) == )
dict.insert(make_pair(abbr, ));
else dict[abbr]++;
}
}
bool isUnique(string word) {
string abbr = getAbbr(word);
if (dict.count(abbr) == || (dict[abbr] == && inDict.count(word) == ))
return true;
return false;
}
};
// Your ValidWordAbbr object will be instantiated and called as such:
// ValidWordAbbr vwa(dictionary);
// vwa.isUnique("hello");
// vwa.isUnique("anotherWord");
要点:int转string可以用函数std::to_string()
inDict 的作用有两个:1)将所给的字典去重。 2)判断要检测的单词是否在所给的字典中。
Unique Word Abbreviation -- LeetCode的更多相关文章
- [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
A string such as "word" contains the following abbreviations: ["word", "1or ...
- [Locked] Unique Word Abbreviation
Unique Word Abbreviation An abbreviation of a word follows the form <first letter><number&g ...
- 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 ...
- [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 ...
- [Swift]LeetCode288. 唯一单词缩写 $ Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
随机推荐
- Python 字符串换行的几种方式
第一种: x0 = '<?xml version="1.0"?>' \ '<ol>' \ ' <li><a href="/pyt ...
- jmeter运行脚本后,请求偶发性的传参错误
问题现象:jmeter写好脚本后,请求偶发性的传参错误 排查过程:1.结合报错返回值,看是不是线程并发引起: 2.排除线程并发引起后,看看是不是取值策略:如果是参数化,看看是不是每次迭代,每次都取唯一 ...
- mongodb 部署
安装mongodb-3.4 1)将安装包上传至服务器 2)对压缩文件进行解压 tar -zxvf mongodb-linux-x86_64-suse12-v3.4-latest.tar.gz 3)把解 ...
- docker 踩坑笔记之 psql: could not connect to server
最近在用docker跑rails,也遇到了一些坑,这里记录一下. 首先build项目: docker-compose build 然后就开始报错了: psql: could not connect t ...
- 整合S2SH框架
S2SH框架(Struts2,Spring,Hibernate)整合 Struts2.Hibernate和Spring.其中在Struts2部分主要为MVC设计思想,Struts2的处理流程及配置,S ...
- hdu 1195 Open the Lock (BFS)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- java链接数据库--Mysql
/************************************************************************* > File Name: Mysql.jav ...
- POJ2187 旋转卡壳 求最长直径
给定平面上的一些散点集,求最远两点距离的平方值. 题解: 旋转卡壳求出凸包,然后根据单调性,求出最远两点的最大距离 #pragma GCC optimize(2) #pragma G++ optimi ...
- 通过RHN网站给RHEL打补丁
[root@yum01 ~]# yum list-sec securityLoaded plugins: downloadonly, product-id, rhnplugin, security, ...
- tomcat + apache 动静分离
原文地址:http://blog.csdn.net/gengv/article/details/5739438 从网上查了不少资料,想了解一下如何整合Apache和Tomcat,以便让Apache的h ...