java 288. Unique Word Abbreviation.java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 288. Unique Word Abbreviation.java相关的知识,希望对你有一定的参考价值。

public class ValidWordAbbr {
    private HashMap<String, String> dict;
    private String getKey(String str) {
        int len = str.length();
        if( len <= 2) return str;
        StringBuilder sb = new StringBuilder();
        sb.append(str.charAt(0)).append(len - 2).append(str.charAt(len - 1));
        return sb.toString();
    }
    public ValidWordAbbr(String[] dictionary) {
        dict = new HashMap<String, String>();
        for(String word : dictionary) {
            String key = getKey(word);
            if(!dict.containsKey(key)) {
                dict.put(key, word);
            } else if(!dict.get(key).equals(word)){
                dict.put(key, "");
            }
        }
    }
    
    public boolean isUnique(String word) {
        String key = getKey(word);
        return !dict.containsKey(key) || dict.get(key).equals(word);
    }
}

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

以上是关于java 288. Unique Word Abbreviation.java的主要内容,如果未能解决你的问题,请参考以下文章

java 288. Unique Word Abbreviation.java

java 288. Unique Word Abbreviation.java

java 288. Unique Word Abbreviation.java

java 288. Unique Word Abbreviation.java

288. Unique Word Abbreviation

288. Unique Word Abbreviation