Why does it give Runtime error?


  • 0
    S
     class Solution{
        public:
            int maxProduct(vector<string>& words){
                 int n=words.size();
                 if(n<2)return 0;
                 sort(words.begin(),words.end(),compare);
                 int lenProduct=0;
                 for(int i=n-1;i>=1;i--){
                      string word1=words[i];
                      int len1=word1.size();
                      if(len1*len1<=lenProduct)break;
                      for(int j=i-1;j>=0;j--){
                          if(len1*words[j].size()<=lenProduct)break;
                          if(NoCommon(word1,words[j])lenProduct=len1*words[j].size();
                      }
                 }
                 return lenProduct;
            }
            int static compare(string S1,string S2){return S1.size()<=S2.size();}
            bool NoCommon(string S1,string S2){
                 set<char> Set1;
                 for(char ch : S1)Set1.insert(ch);
                 for(char ch : S2)if(Set1.find(ch)!=Set1.end())return false;
                 return true;
            }
      };
    

    It seems using self-defined compare() function causes this problem. Can anyone tell me why?


Log in to reply
 

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