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

一刷
题解:用map abbreviation->word来求解,很简单
但是要注意,如果dict里面有word, 有唯一unique, 那么isUnique(word)应该返回true.

所以用一个set来保存所有的dict里面的abbreviation是行不通的。必须保存原先的word。那么如果dict中有两个不同word有同一个abbreviation呢

于是该用Map<String,Set<String>> map;

class ValidWordAbbr {
    private Map<String,Set<String>> abbr;

    public ValidWordAbbr(String[] dictionary) {
        abbr = new HashMap<>();
        for(String str:dictionary){
            String a = getAbb(str);
            if(abbr.containsKey(a)){
                Set<String> s = abbr.get(a);
                if(!s.contains(str)) s.add(str);
            }else{
                Set<String> s = new HashSet();
                s.add(str);
                abbr.put(a,s);
            }
        }
    }
    
    public boolean isUnique(String word) {
        String abb = getAbb(word);
        Set<String> s = abbr.get(abb);
        if(s == null) return true;
        if(s.size()>1) return false;
        if(s.contains(word)) return true;
        return false;
        
    }
    
    public String getAbb(String str){
        if(str.length() <= 2){
                return str;
        }
            
        StringBuilder sb = new StringBuilder(3);
        sb.append(str.charAt(0));
        sb.append(str.length()-2);
        sb.append(str.charAt(str.length()-1));
        return sb.toString();
    }
}

/**
 * Your ValidWordAbbr object will be instantiated and called as such:
 * ValidWordAbbr obj = new ValidWordAbbr(dictionary);
 * boolean param_1 = obj.isUnique(word);
 */

方法二,
用Map<String, String> map; 如果在dict中,不同的word有同一个abbreviation, 那么直接把map的value置为“”

class ValidWordAbbr {
    /*
        If there is more than one string belong to the same key
        then the key will be invalid, we set the value to ""
    */
    Map<String, String> map;
    public ValidWordAbbr(String[] dictionary) {
        map = new HashMap<String, String>();
        for (String word : dictionary) {
            String key = toAbbr(word);
            if (!map.containsKey(key))
                map.put(key, word);
            else if (!map.get(key).equals(word))
                map.put(key, "");
        }
    }
    
    public boolean isUnique(String word) {
        String key = toAbbr(word);
        return !map.containsKey(key) || map.get(key).equals(word);
    }
    
    private String toAbbr(String str) {
        if (str.length() <= 2)
            return str;
        else 
            return str.charAt(0) + String.valueOf(str.length() - 2) + str.charAt(str.length() - 1);
    }
}

/**
 * Your ValidWordAbbr object will be instantiated and called as such:
 * ValidWordAbbr obj = new ValidWordAbbr(dictionary);
 * boolean param_1 = obj.isUnique(word);
 */
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,358评论 0 33
  • An abbreviation of a word follows the form . Below are so...
    我是你的果果呀阅读 1,837评论 0 0
  • 我爱你因为你是我的玫瑰我爱你张扬四射爱你任性刁蛮因为你是我的玫瑰 你太美了总有人觊觎你我总是害怕你被偷盗者抢走你对...
    繁椿阅读 1,738评论 1 3
  • 今日6:00起床后静坐,感觉神清气爽,心绪平静稍许。比此前静坐略有长进。静坐已5月有余矣,除几次稍有感觉外,其余总...
    余良阅读 2,383评论 0 4

友情链接更多精彩内容