288. Unique Word Abbreviation

51 篇文章 0 订阅
23 篇文章 0 订阅

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
 
 
先看下面这道题的解释:

The description (A word's abbreviation is unique if no other word from the dictionary has the same abbreviation) is clear however a bit twisting. It took me a few "Wrong Answer"s to finally understand what it's asking for.
We are trying to search for a word in a dictionary. If this word (also this word’s abbreviation) is not in the dictionary OR this word and only it’s abbreviation in the dictionary. We call a word’s abbreviation unique.
EX:

1) [“dog”]; isUnique(“dig”);   

//False, because “dig” has the same abbreviation with “dog" and “dog” is already in the dictionary. It’s not unique.

2) [“dog”, “dog"]; isUnique(“dog”);  

//True, because “dog” is the only word that has “d1g” abbreviation.

3) [“dog”, “dig”]; isUnique(“dog”);   

//False, because if we have more than one word match to the same abbreviation, this abbreviation will never be unique.

可以理解为把这个字符串加进来会不会有重复,如果里面有这个缩写,而且对应的单词跟这个单词不相等,返回false,相等返回true;如果里面没这个缩写,返回true。在创建词典的时候,如果两个词指向了同一个缩写,把这个缩写对应的单词设成“”,因为这个缩写在词典中都不能唯一,废缩写。代码如下:

public class ValidWordAbbr {
    HashMap<String, String> hm = new HashMap<String, String>();

    public ValidWordAbbr(String[] dictionary) {
        for (int i = 0; i < dictionary.length; i++) {
            String str = trasfstr(dictionary[i]);
            if (hm.containsKey(str) && !dictionary[i].equals(hm.get(dictionary[i]))) {
                hm.put(str, "");
            } else {
                hm.put(str, dictionary[i]);
            }
        }
    }

    public boolean isUnique(String word) {
        String strword = trasfstr(word);
        if (hm.containsKey(strword)) {
            if (word.equals(hm.get(strword))) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }
    
    private String trasfstr(String oldstr) {
        String newstr = "";
        if (oldstr.length() <= 2) {
            newstr = oldstr;
        } else {
            newstr = "" + oldstr.charAt(0) + (oldstr.length() - 2) + oldstr.charAt(oldstr.length() - 1);
        }
        return newstr;
    }
}


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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值