Problem with submission result


  • 0
    W

    I used C Programming language to implement the problem based on the approach of Longest Common Substring. I met some problem when the input is

    "kxuuisplqldxxqanojzyqlaycqwwrpczkymlbpoolybkbluvfkxzzxsoulnfhlhlqzibfhnbryhbkauxsuavnuqlinxrfdwgypsgjmilygtsqptbmfibcfkgdugljwpzjmwnqhtadraplrtlcxeqoniopzbemhkezvadjblpgmyuwlkwilipuccuqfvyzxtoathpnprqphtsiqjlocrmupngjnuskvbzadwxtxchsutumbvidxauotploicaqxegkstdfkyqbmegjhzdrqsuvrspqzbesgzwelrlejlilqvybdjyflbcziqlncddoohurovyuhfhjoyrkxbrvsepxbsivtrahz"

    But actually my output is "elrle" just like expected.

    Could anyone help solving my question?

    Thanks a lot.

    int cnt(char *s)
    {
        int i=0;
        if(s==NULL)
            return 0;
        else
        {
            while(s[i])
                i++;
        }
        return i;
    }
    
    char* convert(char *a)
    {
        int length=cnt(a);
        char *b;
        b = (char*)malloc(length*sizeof(char));
        for(int i=0;i<=length-1;i++)
        {
            b[i]=a[length-1-i];
        }
        char *conversion;
        conversion= (char*)malloc(length*sizeof(char));
        for(int i=0;i<=length-1;i++)
        {
            conversion[i]=b[i];
        }
        return conversion;
    }
    
    char* getCommon(char *a,char *b)
    {
        int len1,len2;
        int i,j,k;
        int max=0;
        int start = -1;
        
        len1 = cnt(a);
        len2 = cnt(b);
        for(i = 0; i < len1; i++)
        {
            for(j = 0; j < len2; j++)
            {
                if(a[i] == b[j])
                {
                    for(k = 0; (a[i+k] == b[j+k] && a[i+k] ); k++);
                    if(max < k && len1-1-i==j+k-1)
                    {
                        max = k;
                        start = i;
                    }
                }
            }
        }
        //printf("max=%d\n",max);
        char *c;
        c = (char*)malloc(max*sizeof(char));
        for(int n=0;n<max;n++)
        {
            c[n]=a[start+n];
        }
        return c;
    }
    
    char* longestPalindrome(char* s) {
        if(cnt(s)==0)
        {return "";}
        else if(cnt(s)==1)
        {return s;}
        else
        {
            char *conversion;
            char *result;
            conversion=convert(s);
            //printf("conversion=%s\n",conversion);
            result=getCommon(s,conversion);
            //printf("result=%s\n",result);
            return result;
        }
    }
    

Log in to reply
 

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