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 ...
随机推荐
- Mysql与Oracle之间的数据类型转换
MySQL Data Type Oracle Data Type BIGINT NUMBER(19, 0) BIT RAW BLOB BLOB, RAW CHAR CHAR DATE DATE DAT ...
- SSH非交互式密码授权远程执行脚本
公司有上百台服务器,需要为每台服务器都执行一个脚本,因为所有服务器的账号密码都是一样的,所以可以不用搭建ansible等自动化运维工具,我们直接通过SSH远程执行即可完成. 本文以三台服务器为例,系统 ...
- 数据分析—NaN数据处理
目的 1.查找NaN值(定位到哪一列.在列的哪个索引位置) 2.填充NaN值(向上填充.向下填充.线性填充等) 3.过滤NaN值 构建简单的Dataframe数据结构环境 import pandas ...
- js万年历
首先,注意: 1.延迟执行 window.setTimeout( , ) 里面的时间是以毫秒计算的 2.间隔执行 window.setInterval( , ...
- HDU 1937 F - Finding Seats 枚举
F - Finding Seats Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- 201621123034 《Java程序设计》第12周学习总结
作业12-流与文件 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 面向系统综合设计-图书馆管理系统或购物车 使用流与文件改造你的图书馆管理系统或购物车 ...
- StringBuilder_学习笔记
参考:https://www.jianshu.com/p/160c9be0b132 连接符号 "+" 本质 字符串变量(非final修饰)通过 "+" 进行拼接 ...
- java中unmodifiableList方法的应用场景
java对象中primitive类型变量可以通过不提供set方法保证不被修改,但对象的List成员在提供get方法后,就可以随意add.remove改变其结构,这不是希望的结果.网上看了下,发现Col ...
- ZOJ 1081 Points Within | 判断点在多边形内
题目: 给个n个点的多边形,n个点按顺序给出,给个点m,判断m在不在多边形内部 题解: 网上有两种方法,这里写一种:射线法 大体的思想是:以这个点为端点,做一条平行与x轴的射线(代码中射线指向x轴正方 ...
- Python实现队列
队列的数据结构的主要结构:一个结点类和两个方法:出队列和进队列 class Node(object): def __init__(self,val): self.val = val self.next ...