288_Unique_Word_Abbreviation


Unique Word Abbreviation

Difficulty Medium

tags string

An abbreviation of a word follows the form

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

这道题的题目描述比较模糊,需要多次尝试test case 来找出规律.

失误的cases:

  1. dictionary 中的单词没有说是no duplicates, 一定要考虑有重复的情况。 否则,这里用计数法来统计单词是否有重复就会出错

  2. 虽然在题目描述中没有给,但是在test case中给了,如果输入了和dict中相同的word, 而这个word在dict中缩写唯一, 就返回true

solution 1


class ValidWordAbbr {
public:
    ValidWordAbbr(vector<string> dictionary) {
        for (string word : dictionary){
            string nw = word_abbr(word);
            if (m.find(nw) == m.end()) 
                m.insert(pair<string, string>(nw, word));
            else if (m[nw] != word)
                m[nw] = "";
            
        }
    }
    
    bool isUnique(string word) {
        string nw = word_abbr(word);
        if (m.find(nw) != m.end() && m[nw] != word)
            return false;
        return true;
    }
private:
    string word_abbr(string w){
        int l = w.size();
        string nw;
        if (l>2) 
            nw = w[0] + to_string(l-2) +  w[l-1];
        else
            nw = w;

        return nw;
    }
    
    map<string, string> m;
};

/**
 * Your ValidWordAbbr object will be instantiated and called as such:
 * ValidWordAbbr obj = new ValidWordAbbr(dictionary);
 * bool param_1 = obj.isUnique(word);
 */
优质内容筛选与推荐>>
1、windows电脑建立wifi热点
2、Application analysis: Northwind.NET
3、数据库的锁定
4、实现网页中增加刷新按钮、链接的方法 搜集
5、python的XML处理模块ElementTree


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号