Leetcode - Unique Word Abbreviation

Leetcode - Unique Word Abbreviation_第1张图片
Screenshot from 2016-02-19 00:45:35.png

My code:

public class ValidWordAbbr {
    HashMap> tracker = new HashMap>();
    public ValidWordAbbr(String[] dictionary) {
        if (dictionary == null || dictionary.length == 0)
            return;
        for (int i = 0; i < dictionary.length; i++) {
            String key = trans(dictionary[i]);
            if (tracker.containsKey(key)) {
                HashSet hs = tracker.get(key);
                hs.add(dictionary[i]);
            }
            else {
                HashSet hs = new HashSet();
                hs.add(dictionary[i]);
                tracker.put(key, hs);
            }
        }
    }

    public boolean isUnique(String word) {
        String key = trans(word);
        if (!tracker.containsKey(key))
            return true;
        else {
            HashSet hs = tracker.get(key);
            if (hs.size() <= 1 && hs.contains(word))
                return true;
            else
                return false;
        }
    }
    
    public String trans(String s) {
        if ( s == null || s.length() <= 2)
            return s;
        return "" + s.charAt(0) + (s.length() - 2) + s.charAt(s.length() - 1);
    }
}


// Your ValidWordAbbr object will be instantiated and called as such:
// ValidWordAbbr vwa = new ValidWordAbbr(dictionary);
// vwa.isUnique("Word");
// vwa.isUnique("anotherWord");

一开始想复杂了。。
今晚很累。不多说了。

Anyway, Good luck, Richardo!

My code:

public class ValidWordAbbr {
    HashMap dic = new HashMap();
    public ValidWordAbbr(String[] dictionary) {
        if (dictionary == null || dictionary.length == 0) {
            return;
        }
        for (String curr : dictionary) {
            String key = extractKey(curr);
            if (dic.containsKey(key)) {
                if (!dic.get(key).equals(curr)) {
                    dic.put(key, "");
                }
            }
            else {
                dic.put(key, curr);
            }
        }
    }

    public boolean isUnique(String word) {
        String key = extractKey(word);
        return !dic.containsKey(key) || dic.get(key).equals(word);
    }
    
    private String extractKey(String s) {
        if (s == null || s.length() <= 2) {
            return s;
        }
        StringBuilder ret = new StringBuilder();
        ret.append(s.charAt(0));
        ret.append("" + (s.length() - 2));
        ret.append(s.charAt(s.length() - 1));
        
        return ret.toString();
    }
}


// Your ValidWordAbbr object will be instantiated and called as such:
// ValidWordAbbr vwa = new ValidWordAbbr(dictionary);
// vwa.isUnique("Word");
// vwa.isUnique("anotherWord");

这道题目有点意思。我的题意有理解错了。后来看的答案。
reference:
https://discuss.leetcode.com/topic/30533/java-solution-with-one-hashmap-string-string-beats-90-of-submissions

他的意思是:
如果dic 不包含当前输入string的abbreviation 或者 当前string与dic中
代表该abbreviation的string是一样时,返回true

总结下 hashmap, hashset, hashtable

hashmap vs hashtable

hashmap supports null key and value
hashtable does not suppot null key or value

hashmap is not sychronized, we should use CurrentHashMap if we want
hashtable is synchronized

hashset can also add null element
hashset extends hashmap
when hashset adds element:
private final Object newObject = new Object();

public boolean add(K k) {
return map.put(k, newObject) == null ? false : true;
}

hashmap put null key into bucket 0

Anyway, Good luck, Richardo! -- 09/02/2016

你可能感兴趣的:(Leetcode - Unique Word Abbreviation)