[522. Longest Uncommon Subsequence II] C++_sort + hash table


  • 0
    class Solution {
    public:
    int findLUSlength(vector<string>& strs) {
        sort(strs.begin(), strs.end(),[](string a, string b){
            return a.size() > b.size() || (a.size() == b.size() && a < b);
        });
        unordered_map<string,int > mp;
        for(int i = 0; i < strs.size(); ++i){mp[strs[i]]++;}
        for(int i = 0; i < strs.size(); ++i){
            if(mp[strs[i]] == 1){
                    int j = 0;
                    for(; j < i; ++j){
                        if(isASubseqOfB(strs[i],strs[j])) break;
                    }
                    if(j == i) return strs[i].size();
            }
        }
        return -1;
    }
    
    bool isASubseqOfB(string& a, string& b){
        if(a.size() > b.size()) return false;
        int i = 0, j = 0;
        while(i < a.size() && j < b.size()){
            if(a[i] == b[j]) i++;
            j++;
        }
        return i == a.size();
    }
    };

Log in to reply
 

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