[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 ...
随机推荐
- php中函数的类型提示和文件读取功能
这个没有深入. <?php function addNumbers(int $a, int $b, bool $printSum): int { $sum = $a + $b; if ($pri ...
- jni接口
https://www.jianshu.com/p/d4a502420a89 #pragma once /*DO NOT EDIT THIS FILE - it is machine generate ...
- 【转】TUN/TAP虚拟网络设备
转: 原文:https://www.cnblogs.com/wyzhou/p/9286864.html ------------------------------------------------ ...
- 关于input标签checkbox属性 和checked
我们设置了type的属性为checkbox时,记住以下3个关键点 1.点勾选时或者说点击时,checked为选中,在input标签中是checked=“checked”,注意这里面无论checked= ...
- Linux操作系统性能调优的方法
http://www.cnblogs.com/L-H-R-X-hehe/p/3963442.html Linux是一套免费使用和自由传播的类Unix操作系统,Linux不同的发行版本和不同的内核对各项 ...
- springboot 打成的jar包在ClassLoader().getResource方法读取文件为null
1.属性文件如下: 10001=错误 2.文件读取主要代码 // getResource方式 URL resourceURI = getClass().getClassLoader().getReso ...
- php+tcpdf如何把生成的pdf文件保存在服务端
tcpdf组件目前应用得非常广泛,但是对于如何把生成的pdf文件自动保存在服务端却很少有人提及.让我们先来看看标准输出代码: //服务器存档模式 $pdf->Output('output.p ...
- LOJ P10002 喷水装置 题解
每日一题 day35 打卡 Analysis 先将不符合条件的区间去掉(即半径小于W,不然宽度无法符合),将符合条件的按区间存入节点中.区间的左边界是x-sqrt(r*r-W*W/4.0),要计算x轴 ...
- BZOJ 4212: 神牛的养成计划 可持久化trie+trie
思路倒是不难,但是这题卡常啊 ~ code: #include <bits/stdc++.h> #define N 2000004 #define M 1000005 #define SI ...
- git log filter(六)
显示前10条提交记录: root@vmuer-VirtualBox:/media/vmuer/share/cmake-uart-server# git log -10 commit b056dacb0 ...