What's the problem of my solution?


  • 0
    V

    Hi, my solution following can not pass certain test cases and I really can not figure out why. Can somebody help me take a quick look? Thanks~

    class Solution {
    public:
        vector<string> wordsAbbreviation(vector<string>& dict) {
            int n = dict.size();
            vector<string> res(n);
            vector<int> pre(n, 1);
            for (int i = 0; i < n; ++i) {
                res[i] = abbreviate(dict[i], pre[i]);
            }
            for (int i = 0; i < n; ++i) {
                set<int> s;
                for (int j  = i; j < n; j++)
                    s.insert(j);
                while (true) {
                    for (auto a : s) {
                        if (res[a] != res[i]) s.erase(a);
                    }
                    if (s.size() == 1) break;
                    for (auto a : s) {
                        res[a] = abbreviate(dict[a], ++pre[a]);
                    }
                }
            }
            return res;
        }
        string abbreviate(string s, int k) {
            return (k >= s.size() - 2) ? s : s.substr(0, k) + to_string(s.size() - k - 1) + s.back();
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.