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 ... 
随机推荐
- javascript学习小记(一)
			大四了,课少了许多,突然之间就不知道学什么啦.整天在宿舍混着日子,很想学习就是感觉没有一点头绪,昨天看了电影激战.这种纠结的情绪让我都有点喘不上气啦!一点要找点事情干了,所以决定找个东西开始学习.那就 ... 
- unity3d中的Viewport
			Camera属性中有个Viewport Rect,如下图: X.Y为(0, 0)代表左下角,(1, 1)代表右上角:W和H分别是Viewport的宽(Width)和高(Height),摄像机的Aspe ... 
- 2. ProGit-Git基础
			(1) 取得项目的Git仓库 从工作目录中初始化新仓库 git init 从现有仓库克隆 git clone ssh协议 http协议 (2) 检查当前文件状态 git status (3) ... 
- 小技巧---查doc文档的index.html怎么用的和chm一样
			看包里面是否有E:\Java\hibernate3.3.2\hibernate-annotations-3.4.0.GA\hibernate-annotations-3.4.0.GA\doc\refe ... 
- 【BZOJ】【1503】 【NOI2004】郁闷的出纳员
			Splay Splay的模板题吧……妥妥的序列操作= =(好像有段时间没写过这种纯数据结构题了……) /************************************************ ... 
- [COCI]coci2015/2016 nekameleoni
			题意: 初始数列,每个数都在1~k以内 支持两种操作:1.修改一个数,修改后的数在1~k内 2.查询一个最短包含1~k的序列的长度 查询100000 ... 
- FbxDataType is ambiguous
			??? 使用fbx自定义的类型的时候,比如 FbxIntDT 会有link error 根本原因是 FbxDataType is ambiguous solution: 把fbx的lib换成 libf ... 
- 2014年03月09日攻击百度贴吧的XSS蠕虫源码
			var n=PageData.user.user_forum_list.info.length; var num=0; var config = { titles: ["\u4f60\u76 ... 
- SSH无密码验证
			一.安装和启动SSH协议 sudo yum install ssh sudo yum install rsync service sshd restart 启动服务 (rsync是一个远程数据同步工具 ... 
- ASP.NET用户控件事件的定义和实践
			假定用户控件(UserControl.ascx)中包含按钮控件 AButton,希望实现按 Button 按钮时,包含该用户控件的页面可以接收到事件. UserControl.ascx.cs ... 
