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的更多相关文章

  1. [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写

    A string such as "word" contains the following abbreviations: ["word", "1or ...

  2. Leetcode Unique Word Abbreviation

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  3. 288. Unique Word Abbreviation

    题目: An abbreviation of a word follows the form <first letter><number><last letter> ...

  4. Unique Word Abbreviation

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  5. [LeetCode] Unique Word Abbreviation 独特的单词缩写

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  6. [Swift]LeetCode288. 唯一单词缩写 $ Unique Word Abbreviation

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  7. Unique Word Abbreviation -- LeetCode

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  8. [LeetCode] 288.Unique Word Abbreviation 独特的单词缩写

    An abbreviation of a word follows the form <first letter><number><last letter>. Be ...

  9. Leetcode: Minimum Unique Word Abbreviation

    A string such as "word" contains the following abbreviations: ["word", "1or ...

随机推荐

  1. codeforce #339(div2)C Peter and Snow Blower

    Peter and Snow Blower 题意:有n(3 <= n <= 100 000)个点的一个多边形,这个多边形绕一个顶点转动,问扫过的面积为多少? 思路:开始就认为是一个凸包的问 ...

  2. 利用jquery的imgAreaSelect插件实现图片裁剪示例

    http://www.cnblogs.com/mizzle/archive/2011/10/13/2209891.html 将用户上传的图片进行裁剪再保存是现在web2.0应用中常常处理的工作,现在借 ...

  3. 8位灰度图在LCD上显示

    一.概述 1.灰度 灰度使用黑色调表示物体,即用黑色为基准色,不同的饱和度的黑色来显示图像.每个灰度对象都具有从 0%(白色)到灰度条100%(黑色)的亮度值. 使用黑白或灰度扫描仪生成的图像通常以灰 ...

  4. javascript高级编程笔记03(正则表达式)

    引用类型 检测数组 注:我们实际开发中经常遇到要把数组转化成以逗号隔开,我以前都是join来实现,其实又更简单的方法可以用toString方法,它会自动用逗号隔开转换成字符串,其实toString内部 ...

  5. CoreBluetooth - TouchID应用

    支持系统和机型: iOS系统的指纹识别功能最低支持的机型为iPhone 5s,最低支持系统为iOS 8, 虽然安装iOS 7系统的5s机型可以使用系统提供的指纹解锁功能,但由于API并未开放,所以理论 ...

  6. Mongodb FAQ fundamentals(基础篇)

    Mongodb FAQ(基础篇),是官方文档的翻译.如有翻译不到之处,还请谅解. 1.Mongdb是什么数据库? mongodb是一个面向文档(document)的数据库,既不支持表连接,也不支持事务 ...

  7. 简单的php表单

    表单的三种传递机制: $_GET:不安全,传递的参数会显示在url中. $_POST:相对安全,隐形传递. $_REQUEST:宽松的,包含所有 GET.POST.COOKIE 和 FILE 的数据. ...

  8. linux运维面试题

    一.有文件file1 1.查询file1 里面空行的所在行号 grep -n "^#" file1 or awk ‗{if($0~/^$/)print NR}‘ file or g ...

  9. SGU481 Hero of Our Time

    Description Saratov ACM ICPC teams have a tradition to come together on Halloween and recollect terr ...

  10. 百度:在O(1)空间复杂度范围内对一个数组中前后连段有序数组进行归并排序

    一.题目理解 题目:数组al[0,mid-1]和al[mid,num-1]是各自有序的,对数组al[0,num-1]的两个子有序段进行merge,得到al[0,num-1]整体有序.要求空间复杂度为O ...