Why runtime error on submit but the failed test case passes when run code


  • 0
    U

    I came across this problem as I submitted the following code. The input that made the runtime error is ["vhsocn","vhsocn","qyaysotbt","dkgq","dkgq","iap","iap","xwqkfowbp","xwqkfowbp","fh","fh","nsx","nsx","myyh","myyh","xzelroamd","xzelroamd","hijbbevisp","hijbbevisp"].
    However when I used the above input under custom testcase, it passed on run code with the same result.
    Any help is appreciated!

    class Solution {
    public:
        int findLUSlength(vector<string>& strs) {
            sort(strs.begin(), strs.end(), cmp);
            set<string> s;
            for(int i=0; i<strs.size(); i++)
            {
                if(i<strs.size()-1 && strs[i]==strs[i+1])
                {
                    s.insert(strs[i]);
                    continue;
                }
                set<string>::iterator it=s.begin();
                while(it!=s.end() && !isSubseq(strs[i], *it)) it++;
                if(it==s.end()) return strs[i].size();
                s.insert(strs[i]);
            }
            return -1;
        }
        
    private:
        bool isSubseq(string a, string b)
        {
            if(a.empty() || a==b) return true;
            int j=0;
            for(int i=0; i<b.size(); i++)
            {
                if(b[i]==a[j]) j++;
                if(j==a.size()) return true;
            }
            return false;
        }
        
        static bool cmp(const string &a, const string &b)
        {
            return a.size()>b.size() || a<b;
        }
    };
    

Log in to reply
 

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