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. GPU CUDA常量内存使用

    #include <cuda.h> #include <stdio.h> int getMulprocessorCount(){ cudaDeviceProp prop; cu ...

  2. 如何使用Promise

    在说Promise之前,不得不说一下JavaScript的嵌套的回调函数 在JavaScript语言中,无论是写浏览器端的各种事件处理回调.ajax回调,还是写Node.js上的业务逻辑,不得不面对的 ...

  3. Automotive Security的一些资料和心得(8):Hardware Security Module (HSM)

    1. Introduction - 保护软件的安全性措施,作为值得信赖的安全锚,- 安全地生成,存储和处理安全性关键材料屏蔽任何潜在的恶意软件,?- 通过运用有效的限制硬件篡改攻击的可能性篡改保护措施 ...

  4. strstr函数与strcmp函数

    1.strstr函数主要完成在一个字串中寻找另外一个字串 函数实现工程如下:摘自http://baike.baidu.com/link?url=RwrzOxs0w68j02J2uQs5u1A56bEN ...

  5. 练习PYTHON之EVENTLET

    以下是重点,要会运用: eventlet是一个用来处理和网络相关的python库函数,而且可以通过协程来实现并发,在eventlet里,把“协程”叫做 greenthread(绿色线程).所谓并发,就 ...

  6. Lua 笔记

    lua命令: #enter shell lua #excute script file lua xxx.lua lua脚本: #!/usr/local/bin/lua 核心概念: As a exten ...

  7. eCos驱动分析 之 ISR是如何与硬件中断联系起来的?

    http://keendawn.blog.163.com/blog/static/8888074320116205833478/

  8. push本地代码到github出错

    $ git push -u origin masterTo git@github.com:******/Demo.git ! [rejected] master -> master (non-f ...

  9. Layout Resource官方教程(3)在layout中用include嵌入其它layout

    简介 <include>Includes a layout file into this layout. 类似 #include ,把layout展开在include处 attribute ...

  10. 【POJ】2823 Sliding Window

    单调队列. /* 2823 */ #include <iostream> #include <sstream> #include <string> #include ...