Runtime error on my C++ qsort based solution

  • 0

    I have tried the bucket-based anagram comparison, but that sometimes were causing unstable results. So, I resorted to sort based anagram solution (using C qsort). Still, I am getting some runtime err.

    Here is what I am doing:

    class Solution {
    static int anacomp(string s1, string s2){
            return s1.length()-s2.length();
        vector<int> ab(26,0);
        for(int i=0;i<s1.length();i++){
        int val = 0;
        int def = 0;
        for(int j=0;j<26;j++){
        if(val!=0 && def==0) def = val;
        return (val==0) ? 0 : def;
    static bool solfuncstrcomp(string s1, string s2){
        return (anacomp(s1,s2)<0);
    static int anagramcomp(const string s1, const string s2){
        int len1 = s1.length();
        int len2 = s2.length();
        if(len1!=len2) return (len1-len2);
        char * str1 = new char [len1+1];
        std::strcpy (str1, s1.c_str());  
        char * str2 = new char [len2+1];
        std::strcpy (str2, s2.c_str());
        return strcmp(str1,str2);
    static int anacompv(const void *s1, const void *s2){    
        string a = *((string*)s1);
        string b = *((string*)s2);
        return anagramcomp(a,b);
    vector<string> anagrams(vector<string> &strs) {
        qsort(&strs[0], strs.size(), sizeof(string), anacompv);
        vector<string> angs;
        int n = strs.size();
        for(int i=0;i<n;i++){
            int preveq=-1, nexteq=+1;
            if(i>0) preveq = (anagramcomp(strs[i],strs[i-1]));
            if(i<(n-1)) nexteq = (anagramcomp(strs[i],strs[i+1]));
        return angs;
    }  };

    What am I doing wrong here? :/
    Anyone can help?

    BTW, I used another std::sort() based version, which was accept. So, what is wrong happening in qsort(), that is causing this RTE? It won't occur in my machine, so can't debug either :(

Log in to reply

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