题目:

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的更多相关文章

  1. [LeetCode] 288.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. [Locked] Unique Word Abbreviation

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

  4. Leetcode Unique Word Abbreviation

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

  5. Unique Word Abbreviation

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

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

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

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

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

  8. Unique Word Abbreviation -- LeetCode

    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. Centering HTML elements larger than their parents

    Centering HTML elements larger than their parents It's not a common problem, but I've run into it a ...

  2. 团队项目--“我爱淘”校园二手书店 NABC分析

    本项目的特点之一:可查询功能 NABC分析: N(Need):方便校园里的学生查找自己需要的二手书籍,免了同学想买二手书还得跑到阿姨那里去看. A(Approach):将学生的信息和书籍的信息都存放在 ...

  3. Careercup - Microsoft面试题 - 6314866323226624

    2014-05-11 05:29 题目链接 原题: Design remote controller for me. 题目:设计一个遥控器. 解法:遥控什么?什么遥控?传统的红外线信号吗?我只能随便说 ...

  4. Shell采集系统cpu 内存 磁盘 网络信息

    cpu信息采集 cpu使用率 采集算法 通过/proc/stat文件采集并计算CPU总使用率或者单个核使用率.以cpu0为例,算法如下: 1. cat /proc/stat | grep ‘cpu0’ ...

  5. Grails 对象关联映射 (GORM) 一

    转自:http://justjavac.iteye.com/blog/701445 Domain 类是任何商业应用的核心. 他们保存事务处理的状态,也处理预期的行为. 他们通过关联联系在一起, one ...

  6. codeforces 22E XOR on Segment 线段树

    题目链接: http://codeforces.com/problemset/problem/242/E E. XOR on Segment time limit per test 4 seconds ...

  7. 推荐系统之LFM(二)

    对于一个用户来说,他们可能有不同的兴趣.就以作者举的豆瓣书单的例子来说,用户A会关注数学,历史,计算机方面的书,用户B喜欢机器学习,编程语言,离散数学方面的书, 用户C喜欢大师Knuth, Jiawe ...

  8. JavaScript之arguments对象讲解

    javascript的arguments对象类似于PHP的extract()函数实现. 在不确定函数参数个数的情况下,可以通过arguments访问参数,并以索引0为起始. function sayH ...

  9. python 小试牛刀之信息管理

    这个是之前写的半成品,但是一直没有好好的写完,今晚我把它补充完整,并且贴出了遇到的问题 这个程序并没有处理中文,主要是python 2.7对于中文的支持太蛋疼了,虽然可以设置utf8编码,但是如果列表 ...

  10. javascript设计模式--单例模式(Singleton)

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...