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
链接: http://leetcode.com/problems/unique-word-abbreviation/
题解:
新题的题目真是越来越长了。 这道题是给定一个数组Dictionary, 求输入字符串是否有unique的abbreviation在Dictionary中。我们选择用Map<String, Set<String>>来做我们存储数据的数据结构,然后按照题意做就可以了,还需要判断一些边界条件,比如缩写不在map中直接返回true之类的。 以后一定要牢记,选定了好的数据结构,编写程序就会容易很多。
Time Complexity - O(n * L), Space Complexity - O(n * L)。
public class ValidWordAbbr {
private Map<String, HashSet<String>> map; public ValidWordAbbr(String[] dictionary) {
this.map = new HashMap<>();
for(int i = 0; i < dictionary.length; i++) {
String abbr = getAbbr(dictionary[i]);
if(!map.containsKey(abbr)) {
HashSet<String> set = new HashSet<>();
set.add(dictionary[i]);
map.put(abbr, set);
} else {
if(!map.get(abbr).contains(dictionary[i])) {
map.get(abbr).add(dictionary[i]);
}
}
}
} public boolean isUnique(String word) {
if(map.size() == 0 || word.length() < 3) {
return true;
}
String abbr = getAbbr(word);
if(!map.containsKey(abbr) || (map.get(abbr).contains(word) && map.get(abbr).size() == 1)) {
return true;
} else {
return false;
}
} private String getAbbr(String s) {
if(s.length() < 3) {
return s;
} else {
return s.substring(0, 1) + String.valueOf(s.length() - 2) + s.substring(s.length() - 1);
}
}
} // Your ValidWordAbbr object will be instantiated and called as such:
// ValidWordAbbr vwa = new ValidWordAbbr(dictionary);
// vwa.isUnique("Word");
// vwa.isUnique("anotherWord");
二刷:
跟一刷的方法一样。就是跟Anagram一样,用Map<String, Set<String>>来存,使用一个新的方法getAbbr先求出abbr作为key,然后把单词加入到key的value里。 Discuss里面还有很多很好的方法,用map<String, String>之类的,三刷要好好研究。
Java:
Time Complexity - O(n * L), Space Complexity - O(n * L)。
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);
}
} // Your ValidWordAbbr object will be instantiated and called as such:
// ValidWordAbbr vwa = new ValidWordAbbr(dictionary);
// vwa.isUnique("Word");
// vwa.isUnique("anotherWord");
Reference:
https://leetcode.com/discuss/62842/a-simple-java-solution-using-map-string-string
https://leetcode.com/discuss/61658/share-my-java-solution
https://leetcode.com/discuss/71652/java-solution-with-hashmap-string-string-beats-submissions
288. Unique Word Abbreviation的更多相关文章
- [LeetCode] 288.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 ...
- [Locked] Unique Word Abbreviation
Unique Word Abbreviation An abbreviation of a word follows the form <first letter><number&g ...
- Leetcode Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [LeetCode] Unique Word Abbreviation 独特的单词缩写
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- [Swift]LeetCode288. 唯一单词缩写 $ Unique Word Abbreviation
An abbreviation of a word follows the form <first letter><number><last letter>. Be ...
- Unique Word Abbreviation -- LeetCode
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 ...
随机推荐
- 《实时控制软件设计》Git 基本操作练习
根据老师提供的教程 对 数据库创建.提交文件.创建分支.删除分支.合并分支.冲突处理等操作进行了练习 得到log文件如下: yanbin-guo@yanbinguo MINGW64 /Git (mas ...
- 使用JavaScript+Html创建win8应用(一)
最近在学习win8 metro app的开发,今天刚刚学了一个小的例子,分享一下 开始之前你需要准备... 1.开发win8应用需要具备Windows 8 和 Microsoft Vi ...
- WEB实时聊天 comet推技术
转自:http://www.cnblogs.com/wodemeng/archive/2012/04/06/2435302.html 今天晚上朋友遇到web服务端推技术的问题,自己就查了下资料,学习了 ...
- IE9兼容性视图与IE9标准视图
如果你使用的是IE9,那么按下F12键就会出现开发者工具,上面有两个下拉菜单:浏览器模式和文档模式.那么什么是浏览器模式?什么又是文档模式?二者有何区别? 浏览器模式用于切换IE针对该网页的默认文档模 ...
- timersmanager 解析
最近在看crtmp源代码,看到timersmanager 模块时感觉很难理解,花了不少时间反复思考该模块 的逻辑,现在思考的结果记录下来,方便以后查阅. 构造函数中将处理时间方法传进来,将_lastT ...
- pm2 开机自动运行
pm2 start app.js -i max -name 可以运行的用户 env PATH=$PATH:/usr/bin pm2 startup -u 可以运行的用户 ubuntu/centos
- 【BZOJ】【3210】花神的浇花集会
曼哈顿距离与切比雪夫距离 QAQ蒟蒻并不知道切比雪夫距离是什么……并不会做这道题…… 去膜拜了PoPoQQQ大爷的题解: 题目大意:给定平面上的n个点,求一个点到这n个点的切比雪夫距离之和最小 与31 ...
- [原]TCP/UDP使用细节备忘
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- Topcoder srm 632 div2
脑洞太大,简单东西就是想复杂,活该一直DIV2; A:水,基本判断A[I]<=A[I-1],ANS++; B:不知道别人怎么做的,我的是100*N*N;没办法想的太多了,忘记是连续的数列 我们枚 ...
- 一道题DP
Problem Description 小明明又被大威鱼抓住了,大威鱼把小明明关在地牢里,地牢由n * n 个房间组成,小明被困在地牢的最左上角的房间中,出口在最右下角,他想逃出这个诡异的地牢,但是他 ...