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. xcode 中添加pch文件

    xcode6以后去掉了pch文件,据说苹果是觉得把头文件加在pch中,会让编译变慢,但是作为我们程序员来说难不倒我们,所以我们手动来添加一下pch文件即可   首先创建一个工程,然后创建一个pch文件 ...

  2. poj 3373 Changing Digits (DFS + 记忆化剪枝+鸽巢原理思想)

    http://poj.org/problem?id=3373 Changing Digits Time Limit: 3000MS   Memory Limit: 65536K Total Submi ...

  3. 使用.net 的Chart控件绘制曲线图

    在进行软件开发过程中我们可能会碰到需要生成图表的情况,在.NET中以前经常用GDI去绘制,虽然效果也不错,自从.NET 4.0开始,专门为绘制图表而生的Chart控件出现了,有了它,就可以轻松的绘制你 ...

  4. matlab高斯模板生成,K是归一化系数,上面是一个半径200的高斯模板用来做MSR

    R3=; F=zeros(*R3+,*R3+); sigma=R3/; r=; :*R3+     :*R3+         r=(x-R3)*(x-R3)+(y-R3)*(y-R3);       ...

  5. Oracle学习笔记1: 表与约束

    1. 登录SQL Plus: 系统用户有哪些: 1. sys,system权限比较高的用户: 2. sysman操作企业管理器使用的. 1.2 的密码是安装oracle是设置的. 3. scott用户 ...

  6. GDB多进程调试(转)

    http://www.cnblogs.com/ggjucheng/archive/2011/12/15/2288710.html GDB 是 linux 系统上常用的 c/c++ 调试工具,功能十分强 ...

  7. 如何设置EditPlus的默认编码utf-8方式

    最近在使用英文版的Editplus写代码的时候,发现中文字符在调试过程中都变成了乱码,发现是Editplus编码设置的问题,Editplus默认编码可能是ANSI,需要将其改成UTF-8.干脆给设置默 ...

  8. [string]Codeforces158C Cd and pwd commands

    题目链接 题意很清楚 和linux的语句是一样的 pwd输出路径 cd进入 ..回上一层目录 此题完全是string的应用 String的用法 vector<string> s; int ...

  9. ASP.NET控件Button (e.CommandArgument的使用方法)

    e.CommandArgument的使用方法 1. 在 Web 窗体页上显示普通按钮 (Button) 控件. <asp:Button id="MyButton" Text= ...

  10. SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-002-AOP术语解析

    一. 1.Advice Advice是切面的要做的操作,它定义了what.when(什么时候要做什么事) aspects have a purpose—a job they’re meant to d ...