[LeetCode] 288.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
一个单词的缩写可以表示成第一个字母+中间字母个数+最后一个字母。给一个单词字典和一个单词,判断这个单词的缩写是唯一的,即字典的单词缩写中没有这个缩写或者有这个缩写但和这个单词是一样的(注意这种情况的处理)。
解法:定义一个函数用来操作缩写单词,对于字典中的所有单词进行缩写并存入另一个哈希表(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 独特的单词缩写的更多相关文章
- [LeetCode] Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
A string such as "word" contains the following abbreviations: ["word", "1or ...
- 408. Valid Word Abbreviation有效的单词缩写
[抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...
- 288. Unique Word Abbreviation
题目: An abbreviation of a word follows the form <first letter><number><last letter> ...
- Leetcode: Minimum Unique Word Abbreviation
A string such as "word" contains the following abbreviations: ["word", "1or ...
- [Locked] Unique Word Abbreviation
Unique Word Abbreviation An abbreviation of a word follows the form <first letter><number&g ...
- [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 ...
- [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 ...
- [Swift]LeetCode288. 唯一单词缩写 $ Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
随机推荐
- PAT甲级1011水题飘过
题目分析:对于输入的数据分三条,选出每条中最大值记录下来,按照题目要求算出最大可能的获利即可 #include<iostream> using namespace std; ]; //k数 ...
- php的希尔排序
算是改进了的插入排序, 从性能时间上来看,也确实更有改进. 但比起php内置的功能,性能还有十倍之差呢 <?php /** * 原理:把排序的数据根据增量分成几个子序列,对子序列进行插入排序, ...
- @getMapping和@postMapping,@RestController 区别
@getMapping和@postMapping,@RestController @RequestMapping 和 @GetMapping @PostMapping 区别 @GetMapp ...
- c#中的多态学习总结
c#的多台方法,大体上和c++的类似,但是有点区别的,我这里刚刚初学,因此把重点记录下. 多态是同一个行为具有多个不同表现形式或形态的能力. 多态性意味着有多重形式.在面向对象编程范式中,多态性往往表 ...
- python的any()函数
any()函数的参数是一个可迭代对象,其中的一个元素有一个为真,则any()函数返回真,除非全部为假的时候才返回假. aaa=[,,,] print(any(aaa)) 返回:false
- 浅谈C++编译原理 ------ C++编译器与链接器工作原理
原文:https://blog.csdn.net/zyh821351004/article/details/46425823 第一篇: 首先是预编译,这一步可以粗略的认为只做了一件事情,那就 ...
- 详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud
转载地址:https://www.jb51.net/article/109382.htm 这篇文章主要介绍了详解如何在CentOS7中使用Nginx和PHP7-FPM安装Nextcloud,会通过 N ...
- Web 项目的文件/文件夹上传下载
我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...
- less简介及其编译原理
一.less环境安装 ①首先需要在电脑上安装nodejs,一般会内置npm,利用以下命令可以检测: ②利用npm在线安装less,运行 npm install –g less ③查看是否安装成功,L ...
- Linux修改服务器Oracle字符集
Linux安装Oracle时太仓促,没设置好,导入dmp字符集(ZHS16GBK)与服务器字符集(WE8MSWIN1252)对不上,导致导入数据失败: [oracle@ORACLE ~]$ sqlpl ...