C++ 3ms


  • 0
    D


    vector<string> r;//record those strings appear more than once
    bool cmp(string a, string b) {//longer strings have high possibility to generate an answer
    return a.length() == b.length() ? a < b : a.length() > b.length();
    }
    bool match(string test) {//whether test is the subsequence of strings in r
    for (int i = r.size()-1; i>=0; i--)//reverse search can be efficiency
    for (int p = 0, q = 0; p<r[i].length(); p++) {
    if (r[i][p] == test[q])
    q++;
    if (q == test.length())
    return true;
    }
    return false;
    }
    int findLUSlength(vector<string>& strs) {
    sort(strs.begin(), strs.end(),cmp);
    for (int i = 0; i < strs.size()-1; i++) {
    if (match(strs[i])) continue;
    if (strs[i] == strs[i + 1]) r.push_back(strs[i]);
    else return strs[i].length();
    }
    if(!match(strs[strs.size()-1])) return strs[strs.size()-1].length();
    return -1;
    }


Log in to reply
 

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