LintCode 639: Word Abbreviation

  1. Word Abbreviation

Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations for every word following rules below.

Begin with the first character and then the number of characters abbreviated, which followed by the last character.
If there are any conflict, that is more than one words share the same abbreviation, a longer prefix is used instead of only the first character until making the map from word to abbreviation become unique. In other words, a final abbreviation cannot map to more than one original words.
If the abbreviation doesn’t make the word shorter, then keep it as original.
Example
Example 1:

Input:
[“like”,“god”,“internal”,“me”,“internet”,“interval”,“intension”,“face”,“intrusion”]
Output:
[“l2e”,“god”,“internal”,“me”,“i6t”,“interval”,“inte4n”,“f2e”,“intr4n”]
Example 2:

Input:
[“where”,“there”,“is”,“beautiful”,“way”]
Output:
[“w3e”,“t3e”,“is”,“b7l”,“way”]
Notice
Both n and the length of each word will not exceed 400.
The length of each word is greater than 1.
The words consist of lowercase English letters only.
The return answers should be in the same order as the original array.

解法1:
代码如下:

注意:
1)当遇到有duplidate时,就再往下进行一个字符,直到没有冲突为止。
2)abbreviate() 应该是round + 2 >= word.size()就不用往下进行了。因为word会不断跟着round改变。如果仅仅用if (n <= 3)是不对的,因为interval和internal最后都是interv1l就不对了。
3)abbrMap里面的旧result[i]不用删掉,其实vector()也不好删,也没有必要删。

class Solution {
public:
    /**
     * @param dict: an array of n distinct non-empty strings
     * @return: an array of minimal possible abbreviations for every word
     */
    vector<string> wordsAbbreviation(vector<string> &dict) {
        int n = dict.size();
        int round = 1;
        vector<string> result(n);
        
        for (int i = 0; i < n; ++i) {
            result[i] = abbreviate(dict[i], round);
            abbrMap[result[i]].push_back(i);
        }
        
        while(1) {
            bool duplicate = false;
            round++;
            for (int i = 0; i < n; ++i) {
                if (abbrMap[result[i]].size() > 1) {
                    duplicate = true;
                    //abbrMap[result[i]]--;
                    result[i] = abbreviate(dict[i], round);
                    
                    abbrMap[result[i]].push_back(i);
                }
            }

            if (!duplicate) break;
        }
        
        return result;
    }

private:
    map<string, vector<int>> abbrMap;  //abbr, vector of index of original words

    string abbreviate(string word, int round) {
        int n = word.size();
        //if (n <= 3) return word;
        if (round + 2 >= word.size()) return word;
        return word.substr(0, round) + to_string(n - round - 1) + word[n - 1];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值