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);
 */
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 背景 一年多以前我在知乎上答了有關LeetCode的問題, 分享了一些自己做題目的經驗。 張土汪:刷leetcod...
    土汪閱讀 12,766評論 0 33
  • An abbreviation of a word follows the form . Below are so...
    我是你的果果呀閱讀 272評論 0 0
  • 我愛你因為你是我的玫瑰我愛你張揚四射愛你任性刁蠻因為你是我的玫瑰 你太美了總有人覬覦你我總是害怕你被偷盜者搶走你對...
    繁椿閱讀 253評論 1 3
  • 今日6:00起床后靜坐,感覺神清氣爽,心緒平靜稍許。比此前靜坐略有長進。靜坐已5月有余矣,除幾次稍有感覺外,其余總...
    余良閱讀 292評論 0 4