Longest Palindromic Substring, I don't know why I can't pass.


  • 0
    K

    bool isPalindrme(char* s, int begin, int end)
    {
    while(begin < end)
    {
    if(s[begin] != s[end])
    break;
    else
    {
    begin++;
    end--;
    }
    }
    if(begin < end)
    return false;
    else
    return true;
    }
    char* longestPalindrome(char* s)
    {
    if(s == NULL)
    return NULL;
    char * str = (char *) malloc(1000 * sizeof(char));
    memset(str, 0, sizeof(str));
    int len = strlen(s);
    int i, j, begin=0, end=0;
    int max = 0;
    for(i = 0; i < len; i++)
    {
    for(j = 0; j < i; j++);
    {
    if(isPalindrme(s, j, i))
    {
    if(i-j + 1 > max)
    {
    max = i - j + 1;
    begin = j;
    end = i;
    }
    }
    }
    }
    memcpy(str,&s[begin], max);

    return str;
    

    }

    Your input

    "babad"
    Your answer

    "b"
    Expected answer

    "aba"


Log in to reply
 

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