C++ straight-forward solution

  • 0
    int findLUSlength(vector<string>& strs) {
            int max=0;
            for(int i=0;i<strs.size();i++) {
                int flag=0;
                for(int j=0;j<strs.size();j++) {
                    if(i==j) continue;
                    else if(strs[j].size()<strs[i].size()) continue;
                    else if(check(strs[j], strs[i])) {
                if(flag==0&&strs[i].size()>max) max=strs[i].size();
            if(max==0) return -1;
            return max;
        bool check(string a, string b) {
            int pos=0;
            for(int i=0;i<a.size();i++) {
                if(a[i]==b[pos]&&pos<b.size()) pos++;
            return pos==b.size();

Log in to reply

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