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來(lái)求解,很簡(jiǎn)單
但是要注意,如果dict里面有word, 有唯一unique, 那么isUnique(word)應(yīng)該返回true.

所以用一個(gè)set來(lái)保存所有的dict里面的abbreviation是行不通的。必須保存原先的word。那么如果dict中有兩個(gè)不同word有同一個(gè)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有同一個(gè)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);
 */
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問(wèn)題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,936評(píng)論 0 33
  • An abbreviation of a word follows the form . Below are so...
    我是你的果果呀閱讀 317評(píng)論 0 0
  • 我愛(ài)你因?yàn)槟闶俏业拿倒逦覑?ài)你張揚(yáng)四射愛(ài)你任性刁蠻因?yàn)槟闶俏业拿倒?你太美了總有人覬覦你我總是害怕你被偷盜者搶走你對(duì)...
    繁椿閱讀 271評(píng)論 1 3
  • 今日6:00起床后靜坐,感覺(jué)神清氣爽,心緒平靜稍許。比此前靜坐略有長(zhǎng)進(jìn)。靜坐已5月有余矣,除幾次稍有感覺(jué)外,其余總...
    余良閱讀 347評(píng)論 0 4

友情鏈接更多精彩內(nèi)容