two hashmap C++


  • 0
    B
    int findLUSlength(vector<string>& strs) {
        unordered_map<int,vector<string>> map;
        unordered_map<string,int> c;
        for(auto x:strs){map[x.length()].push_back(x);c[x]++;}
        for(int i=10;i>0;i--)
        {
            int result=find_next(map,i,c);
            if(result>0)return result;
        }
        return -1;
    }
    int find_next(unordered_map<int,vector<string>> &map,int index,unordered_map<string,int> &c)
    {
        auto temp=map[index];
        for(auto x:temp)
        {
            if(c[x]==1 && is_unique(x,map))return x.length();
        }
        return -1;
    }
    bool is_unique(string str,unordered_map<int,vector<string>> &map)
    {
        for(int i=10;i>str.length();i--)for(auto x:map[i])if(is_sub(str,x))return false;
        return true;
    }
    bool is_sub(string x,string y)
    {
        int i=0,j=0;
        while(i<x.length() && j<y.length())if(x[i]==y[j++])i++;
        return i==x.length();
    }

Log in to reply
 

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