[Locked] Unique Word Abbreviation
2016-02-29 11:34
369 查看
Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Below are some examples of word abbreviations:
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:
isUnique("cart") ->
isUnique("cane") ->
isUnique("make") ->
分析:
其实题目没有表达清楚...应该包含如下意思:
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。
代码:
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") -> [code]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[0]; //若只有一位,则直接返回;有两位,则中间不加数字;两个以上,则加数字 if(str.length() > 1) { abbr += str.length() > 2 ? to_string(str.length() - 2) : ""; 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); } };
相关文章推荐
- 48.Which two statements are true regarding indexes? (Choose two.)
- 自定义UINavigationItem的两种方法以及相应的隐藏方法
- UITableView介绍 之 基本使用
- iOS9 UIAlertController简单使用代码
- [Locked] Range Sum Query 2D - Mutable
- UIKit框架(7)应用程序对象UIApplication
- unique函数
- UITableView 学习整理
- 关于kendo ui的使用改变颜色方式
- 限定UITextfield的输入字符的格式和字数
- UIBUtton 的两种图片设置
- UIBUtton 的两种图片设置
- 十八、UiAutomator 相关JAVA知识
- iOS蓝牙4.0 CoreBluetooth框架开发
- Android优化——UI优化(五) Listview 重用convertView
- UISwitch控件修改大小。设置颜色
- UIKit框架(6)控制器view的加载方式
- [转] DBCP 的validationQuery
- ueditor 配置和上传图片
- 别人的jqueyr解读