288. Unique Word Abbreviation

    /*
     * 288. Unique Word Abbreviation
     * 2016-7-1 by Mingyang
     * 这个题目的解释也是醉了,不解释
     */
     class ValidWordAbbr {
            HashMap<String, String> map;
            public ValidWordAbbr(String[] dictionary) {
                map = new HashMap<String, String>();
                for(String str:dictionary){
                    String key = getKey(str);
                    // If there is more than one string belong to the same key
                    // then the key will be invalid, we set the value to ""
                    if(map.containsKey(key)){
                        if(!map.get(key).equals(str)){
                            map.put(key, "");
                        }
                    }
                    else{
                        map.put(key, str);
                    }
                }
            }

            public boolean isUnique(String word) {
                return !map.containsKey(getKey(word))||map.get(getKey(word)).equals(word);
            }

            String getKey(String str){
                if(str.length()<=2) return str;
                return str.charAt(0)+Integer.toString(str.length()-2)+str.charAt(str.length()-1);
            }
        }
原文地址:https://www.cnblogs.com/zmyvszk/p/5634928.html