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

一个单词的缩写可以表示成第一个字母+中间字母个数+最后一个字母。给一个单词字典和一个单词,判断这个单词的缩写是唯一的,即字典的单词缩写中没有这个缩写或者有这个缩写但和这个单词是一样的(注意这种情况的处理)。

解法:定义一个函数用来操作缩写单词,对于字典中的所有单词进行缩写并存入另一个哈希表(key为缩写后的单词,value为set)。再对单词进行缩写,然后判断单词的缩写是否在哈希表中出现,如果没出现那肯定是唯一的。如果出现了还要看set里存的是不是只是这个单词,如果有其它单词出现就不是唯一的。

Java:

public class ValidWordAbbr {
Map<String, Set<String>> map;
public ValidWordAbbr(String[] dictionary) {
map = new HashMap<>();
for (String s : dictionary) {
String abbr = getAbbr(s);
if (!map.containsKey(abbr)) {
map.put(abbr, new HashSet<String>());
}
map.get(abbr).add(s);
}
} public boolean isUnique(String word) {
String abbr = getAbbr(word);
if (!map.containsKey(abbr) || (map.get(abbr).contains(word) && map.get(abbr).size() == 1)) {
return true;
}
return false;
} private String getAbbr(String s) {
if (s.length() < 3) {
return s;
}
int len = s.length();
return s.substring(0, 1) + (len - 2) + s.substring(len - 1);
}
}  

Python:

class ValidWordAbbr(object):
def __init__(self, dictionary):
"""
initialize your data structure here.
:type dictionary: List[str]
"""
self.lookup_ = collections.defaultdict(set)
for word in dictionary:
abbr = self.abbreviation(word)
self.lookup_[abbr].add(word) def isUnique(self, word):
"""
check if a word is unique.
:type word: str
:rtype: bool
"""
abbr = self.abbreviation(word)
return self.lookup_[abbr] <= {word} def abbreviation(self, word):
if len(word) <= 2:
return word
return word[0] + str(len(word)-2) + word[-1]

C++:

// Time:  ctor:   O(n), n is number of words in the dictionary.
// lookup: O(1)
// Space: O(k), k is number of unique words. class ValidWordAbbr {
public:
ValidWordAbbr(vector<string> &dictionary) {
for (string& word : dictionary) {
const string abbr = abbreviation(word);
lookup_[abbr].emplace(word);
}
} bool isUnique(string word) {
const string abbr = abbreviation(word);
return lookup_[abbr].empty() ||
(lookup_[abbr].count(word) == lookup_[abbr].size());
} private:
unordered_map<string, unordered_set<string>> lookup_; string abbreviation(const string& word) {
if (word.length() <= 2) {
return word;
}
return word.front() + to_string(word.length()) + word.back();
}
};

  

  

  

All LeetCode Questions List 题目汇总

[LeetCode] 288.Unique Word Abbreviation 独特的单词缩写的更多相关文章

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

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

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

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

  3. 408. Valid Word Abbreviation有效的单词缩写

    [抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...

  4. 288. Unique Word Abbreviation

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

  5. Leetcode: Minimum Unique Word Abbreviation

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

  6. [Locked] Unique Word Abbreviation

    Unique Word Abbreviation An abbreviation of a word follows the form <first letter><number&g ...

  7. [LeetCode] 244. Shortest Word Distance II 最短单词距离 II

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  8. [LeetCode] 245. Shortest Word Distance III 最短单词距离 III

    This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...

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

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

随机推荐

  1. 【学英语~磨耳朵】2013年以来看过的所有美剧&电影&纪录片等等

    我看美剧看太多了,而且同一部剧刷很多遍.这种coach potato的做法其实一点也不好,英文会好可能只是意外收获.下面是单子: 美剧: 老友记-情景喜剧-10季全看.至今还在网易云音乐循环10季音频 ...

  2. springboot 2.x整合redis,spring aop实现接口缓存

    pox.xml: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  3. 韩顺平老师java视频全套-java视频教程下载

    解压压缩包会有一个种子文件.直接迅雷下载即可,包含了韩顺平老师的java入门视频,jdbc,jsp,servlet,oracle,hibermate,spring,SHH框架,struct,linux ...

  4. php+tcpdf生成pdf: 中文乱码

    TCPDF是一个生成PDF的不错的库,可惜,官方对包括中文在内的东亚字体支持不怎么样的.场景:某项目需要根据数据库信息生成pdf格式的发票,考虑采用稳定的tcpdf,虽然还有许多其它选择,但是这个应该 ...

  5. Codeforces Round #605 (Div. 3) E. Nearest Opposite Parity(最短路)

    链接: https://codeforces.com/contest/1272/problem/E 题意: You are given an array a consisting of n integ ...

  6. 编程用泰勒公式求e的近似值,直到最后一项小于10的负6次方为止。

    #include<stdio.h>#include<math.h>void main(){ int n; float j=1.0,sum=1.0; for(n=1;;n++) ...

  7. SpringBoot学习(五)RSocket和Security

    一.RSocket RSocket是一个用于字节流传输的二进制协议.它通过在单个连接上传递异步消息来支持对称交互模型,主要支持的通讯层包括 TCP, WebSockets和Aeron(UDP). RS ...

  8. 4个MySQL优化工具AWR,帮你准确定位数据库瓶颈!(转载)

    对于正在运行的mysql,性能如何,参数设置的是否合理,账号设置的是否存在安全隐患,你是否了然于胸呢? 俗话说工欲善其事,必先利其器,定期对你的MYSQL数据库进行一个体检,是保证数据库安全运行的重要 ...

  9. The two of the oldest man need cheers

    At a company dinner, the drinking rule was that two colleagues of similar age clinked glasses of win ...

  10. (转)React事件处理函数必须使用bind(this)的原因

    1.JavaScript自身特性说明如果传递一个函数名给一个变量,之后通过函数名()的方式进行调用,在方法内部如果使用this则this的指向会丢失.示例代码:首先我们创建test对象并直接调用方法 ...