5. longest palindrome substring (leetcode) – C language

Time:2022-6-8

1、 Brute force cracking

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//Determine whether the string is a palindrome string
int isPalindromic(char *str, int start, int end)
{
  while (start < end)
  {
    if (str[start] != str[end])
    {
      return false;
    }
    start++;
    end--;
  }
  return true;
}

char *longestPalindrome(char *str)
{
  int len = strlen(str);
  int k = 0;
  if (len < 2)
  {
    return str;
  }
  //The minimum length of palindrome string is 1, because any character read from left to right or from right to left is palindrome
  //Therefore, the substring with length of 1 is palindrome, and the first character is returned by default
  int maxLen = 1, startIndex = 0;
  //Traverse all substrings with length >=2. If not, use the above default value, that is
  //Returns a substring of length 1 and is the first character of the string
  for (int i = 0; i < len - 1; i++)
  {
    for (int j = i + 1; j < len; j++)
    {
      //When the length of the new substring is greater than maxlen, judge whether the palindrome
      //If the palindrome, update maxlen and StartIndex
      if (j - i + 1 > maxLen && isPalindromic(str, i, j))
      {
        maxLen = j - i + 1;
        startIndex = i;
      }
    }
  }
  //The following two lines of code can reduce the space complexity to o (1)
  // str[startIndex + maxLen] = '
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//Determine whether the string is a palindrome string
int isPalindromic(char *str, int start, int end)
{
while (start < end)
{
if (str[start] != str[end])
{
return false;
}
start++;
end--;
}
return true;
}
char *longestPalindrome(char *str)
{
int len = strlen(str);
int k = 0;
if (len < 2)
{
return str;
}
//The minimum length of palindrome string is 1, because any character read from left to right or from right to left is palindrome
//Therefore, the substring with length of 1 is palindrome, and the first character is returned by default
int maxLen = 1, startIndex = 0;
//Traverse all substrings with length >=2. If not, use the above default value, that is
//Returns a substring of length 1 and is the first character of the string
for (int i = 0; i < len - 1; i++)
{
for (int j = i + 1; j < len; j++)
{
//When the length of the new substring is greater than maxlen, judge whether the palindrome
//If the palindrome, update maxlen and StartIndex
if (j - i + 1 > maxLen && isPalindromic(str, i, j))
{
maxLen = j - i + 1;
startIndex = i;
}
}
}
//The following two lines of code can reduce the space complexity to o (1)
// str[startIndex + maxLen] = '\0';
// return str + startIndex;
//Allocate storage space, store new string and return
//One more space should be allocated to store the empty character'\0'during allocation
char *palindromic = (char *)malloc((maxLen + 1) * sizeof(char));
for (int i = 0; i < maxLen; i++)
{
palindromic[i] = str[startIndex + i];
}
palindromic[maxLen] = '\0';
return palindromic;
}
int main(void)
{
int i = 0;
char str[] = "babad";
char *palind = NULL;
palind = longestPalindrome(str);
while (palind[i] != '\0')
{
printf("%c", palind[i]);
i++;
}
}
'; // return str + startIndex; //Allocate storage space, store new string and return //One more space should be allocated to store the empty character'
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//Determine whether the string is a palindrome string
int isPalindromic(char *str, int start, int end)
{
while (start < end)
{
if (str[start] != str[end])
{
return false;
}
start++;
end--;
}
return true;
}
char *longestPalindrome(char *str)
{
int len = strlen(str);
int k = 0;
if (len < 2)
{
return str;
}
//The minimum length of palindrome string is 1, because any character read from left to right or from right to left is palindrome
//Therefore, the substring with length of 1 is palindrome, and the first character is returned by default
int maxLen = 1, startIndex = 0;
//Traverse all substrings with length >=2. If not, use the above default value, that is
//Returns a substring of length 1 and is the first character of the string
for (int i = 0; i < len - 1; i++)
{
for (int j = i + 1; j < len; j++)
{
//When the length of the new substring is greater than maxlen, judge whether the palindrome
//If the palindrome, update maxlen and StartIndex
if (j - i + 1 > maxLen && isPalindromic(str, i, j))
{
maxLen = j - i + 1;
startIndex = i;
}
}
}
//The following two lines of code can reduce the space complexity to o (1)
// str[startIndex + maxLen] = '\0';
// return str + startIndex;
//Allocate storage space, store new string and return
//One more space should be allocated to store the empty character'\0'during allocation
char *palindromic = (char *)malloc((maxLen + 1) * sizeof(char));
for (int i = 0; i < maxLen; i++)
{
palindromic[i] = str[startIndex + i];
}
palindromic[maxLen] = '\0';
return palindromic;
}
int main(void)
{
int i = 0;
char str[] = "babad";
char *palind = NULL;
palind = longestPalindrome(str);
while (palind[i] != '\0')
{
printf("%c", palind[i]);
i++;
}
}
'during allocation char *palindromic = (char *)malloc((maxLen + 1) * sizeof(char)); for (int i = 0; i < maxLen; i++) { palindromic[i] = str[startIndex + i]; } palindromic[maxLen] = '
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//Determine whether the string is a palindrome string
int isPalindromic(char *str, int start, int end)
{
while (start < end)
{
if (str[start] != str[end])
{
return false;
}
start++;
end--;
}
return true;
}
char *longestPalindrome(char *str)
{
int len = strlen(str);
int k = 0;
if (len < 2)
{
return str;
}
//The minimum length of palindrome string is 1, because any character read from left to right or from right to left is palindrome
//Therefore, the substring with length of 1 is palindrome, and the first character is returned by default
int maxLen = 1, startIndex = 0;
//Traverse all substrings with length >=2. If not, use the above default value, that is
//Returns a substring of length 1 and is the first character of the string
for (int i = 0; i < len - 1; i++)
{
for (int j = i + 1; j < len; j++)
{
//When the length of the new substring is greater than maxlen, judge whether the palindrome
//If the palindrome, update maxlen and StartIndex
if (j - i + 1 > maxLen && isPalindromic(str, i, j))
{
maxLen = j - i + 1;
startIndex = i;
}
}
}
//The following two lines of code can reduce the space complexity to o (1)
// str[startIndex + maxLen] = '\0';
// return str + startIndex;
//Allocate storage space, store new string and return
//One more space should be allocated to store the empty character'\0'during allocation
char *palindromic = (char *)malloc((maxLen + 1) * sizeof(char));
for (int i = 0; i < maxLen; i++)
{
palindromic[i] = str[startIndex + i];
}
palindromic[maxLen] = '\0';
return palindromic;
}
int main(void)
{
int i = 0;
char str[] = "babad";
char *palind = NULL;
palind = longestPalindrome(str);
while (palind[i] != '\0')
{
printf("%c", palind[i]);
i++;
}
}
'; return palindromic; } int main(void) { int i = 0; char str[] = "babad"; char *palind = NULL; palind = longestPalindrome(str); while (palind[i] != '
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//Determine whether the string is a palindrome string
int isPalindromic(char *str, int start, int end)
{
while (start < end)
{
if (str[start] != str[end])
{
return false;
}
start++;
end--;
}
return true;
}
char *longestPalindrome(char *str)
{
int len = strlen(str);
int k = 0;
if (len < 2)
{
return str;
}
//The minimum length of palindrome string is 1, because any character read from left to right or from right to left is palindrome
//Therefore, the substring with length of 1 is palindrome, and the first character is returned by default
int maxLen = 1, startIndex = 0;
//Traverse all substrings with length >=2. If not, use the above default value, that is
//Returns a substring of length 1 and is the first character of the string
for (int i = 0; i < len - 1; i++)
{
for (int j = i + 1; j < len; j++)
{
//When the length of the new substring is greater than maxlen, judge whether the palindrome
//If the palindrome, update maxlen and StartIndex
if (j - i + 1 > maxLen && isPalindromic(str, i, j))
{
maxLen = j - i + 1;
startIndex = i;
}
}
}
//The following two lines of code can reduce the space complexity to o (1)
// str[startIndex + maxLen] = '\0';
// return str + startIndex;
//Allocate storage space, store new string and return
//One more space should be allocated to store the empty character'\0'during allocation
char *palindromic = (char *)malloc((maxLen + 1) * sizeof(char));
for (int i = 0; i < maxLen; i++)
{
palindromic[i] = str[startIndex + i];
}
palindromic[maxLen] = '\0';
return palindromic;
}
int main(void)
{
int i = 0;
char str[] = "babad";
char *palind = NULL;
palind = longestPalindrome(str);
while (palind[i] != '\0')
{
printf("%c", palind[i]);
i++;
}
}
') { printf("%c", palind[i]); i++; } }

Execution time: 304 MS, beating 26.13% of users in all C submissions
Memory consumption: 5.9 MB, beating 79.53% of users in all C submissions
Time complexity O (n^3), space complexity O (n).
This method only returns one solution, that is, there may be multiple longest palindrome strings, and this method only returns the first traversal.

Central diffusion method

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//Central diffusion method
int expandCenter(char *str, int left, int right, int len)
{
  //If the length is an odd number, there is only one center, and left and right pass the same value
  //If the length is an even number, the center is two numbers, and the difference between left and right is 1
  while (left >= 0 && right < len)
  {
    if (str[left] == str[right])
    {
      left--;
      right++;
    }
    else
    {
      break;
    }
  }
  return right - left - 1;
}

char *longestPalindrome(char *str)
{
  int len = strlen(str);
  if (len < 2)
  {
    return str;
  }
  int maxLen = 1;
  int startIndex = 0;
  for (int i = 0; i < len; i++)
  {
    //Called when the palindrome string length is an odd number
    int odd = expandCenter(str, i, i, len);
    //Called when the palindrome string length is even
    int even = expandCenter(str, i, i + 1, len);
    //Assign the longest to maxlen
    int newMaxLen = odd > even ? odd : even;
    if (newMaxLen > maxLen)
    {
      maxLen = newMaxLen;
      startIndex = i - (maxLen - 1) / 2;
    }
  }
  
  //The following two lines of code can reduce the space complexity to o (1)
  // str[startIndex + maxLen] = '
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//Central diffusion method
int expandCenter(char *str, int left, int right, int len)
{
//If the length is an odd number, there is only one center, and left and right pass the same value
//If the length is an even number, the center is two numbers, and the difference between left and right is 1
while (left >= 0 && right < len)
{
if (str[left] == str[right])
{
left--;
right++;
}
else
{
break;
}
}
return right - left - 1;
}
char *longestPalindrome(char *str)
{
int len = strlen(str);
if (len < 2)
{
return str;
}
int maxLen = 1;
int startIndex = 0;
for (int i = 0; i < len; i++)
{
//Called when the palindrome string length is an odd number
int odd = expandCenter(str, i, i, len);
//Called when the palindrome string length is even
int even = expandCenter(str, i, i + 1, len);
//Assign the longest to maxlen
int newMaxLen = odd > even ? odd : even;
if (newMaxLen > maxLen)
{
maxLen = newMaxLen;
startIndex = i - (maxLen - 1) / 2;
}
}
//The following two lines of code can reduce the space complexity to o (1)
// str[startIndex + maxLen] = '\0';
// return str + startIndex;
char *palindromic = (char *)malloc((maxLen + 1) * sizeof(char));
for (int i = 0; i < maxLen; i++) {
palindromic[i] = str[i + startIndex];
}
palindromic[maxLen] = '\0';
return palindromic;
}
int main(void)
{
char str[] = "abcdefghgfedklmn";
char *palind = longestPalindrome(str);
int i = 0;
while (palind[i] != '\0') {
printf("%c", palind[i]);
i++;
}
}
'; // return str + startIndex; char *palindromic = (char *)malloc((maxLen + 1) * sizeof(char)); for (int i = 0; i < maxLen; i++) { palindromic[i] = str[i + startIndex]; } palindromic[maxLen] = '
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//Central diffusion method
int expandCenter(char *str, int left, int right, int len)
{
//If the length is an odd number, there is only one center, and left and right pass the same value
//If the length is an even number, the center is two numbers, and the difference between left and right is 1
while (left >= 0 && right < len)
{
if (str[left] == str[right])
{
left--;
right++;
}
else
{
break;
}
}
return right - left - 1;
}
char *longestPalindrome(char *str)
{
int len = strlen(str);
if (len < 2)
{
return str;
}
int maxLen = 1;
int startIndex = 0;
for (int i = 0; i < len; i++)
{
//Called when the palindrome string length is an odd number
int odd = expandCenter(str, i, i, len);
//Called when the palindrome string length is even
int even = expandCenter(str, i, i + 1, len);
//Assign the longest to maxlen
int newMaxLen = odd > even ? odd : even;
if (newMaxLen > maxLen)
{
maxLen = newMaxLen;
startIndex = i - (maxLen - 1) / 2;
}
}
//The following two lines of code can reduce the space complexity to o (1)
// str[startIndex + maxLen] = '\0';
// return str + startIndex;
char *palindromic = (char *)malloc((maxLen + 1) * sizeof(char));
for (int i = 0; i < maxLen; i++) {
palindromic[i] = str[i + startIndex];
}
palindromic[maxLen] = '\0';
return palindromic;
}
int main(void)
{
char str[] = "abcdefghgfedklmn";
char *palind = longestPalindrome(str);
int i = 0;
while (palind[i] != '\0') {
printf("%c", palind[i]);
i++;
}
}
'; return palindromic; } int main(void) { char str[] = "abcdefghgfedklmn"; char *palind = longestPalindrome(str); int i = 0; while (palind[i] != '
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//Central diffusion method
int expandCenter(char *str, int left, int right, int len)
{
//If the length is an odd number, there is only one center, and left and right pass the same value
//If the length is an even number, the center is two numbers, and the difference between left and right is 1
while (left >= 0 && right < len)
{
if (str[left] == str[right])
{
left--;
right++;
}
else
{
break;
}
}
return right - left - 1;
}
char *longestPalindrome(char *str)
{
int len = strlen(str);
if (len < 2)
{
return str;
}
int maxLen = 1;
int startIndex = 0;
for (int i = 0; i < len; i++)
{
//Called when the palindrome string length is an odd number
int odd = expandCenter(str, i, i, len);
//Called when the palindrome string length is even
int even = expandCenter(str, i, i + 1, len);
//Assign the longest to maxlen
int newMaxLen = odd > even ? odd : even;
if (newMaxLen > maxLen)
{
maxLen = newMaxLen;
startIndex = i - (maxLen - 1) / 2;
}
}
//The following two lines of code can reduce the space complexity to o (1)
// str[startIndex + maxLen] = '\0';
// return str + startIndex;
char *palindromic = (char *)malloc((maxLen + 1) * sizeof(char));
for (int i = 0; i < maxLen; i++) {
palindromic[i] = str[i + startIndex];
}
palindromic[maxLen] = '\0';
return palindromic;
}
int main(void)
{
char str[] = "abcdefghgfedklmn";
char *palind = longestPalindrome(str);
int i = 0;
while (palind[i] != '\0') {
printf("%c", palind[i]);
i++;
}
}
') { printf("%c", palind[i]); i++; } }

Execution time: 20 ms, beating 80.27% of users in all C submissions
Memory consumption: 6 MB, beating 68.40% of users in all C submissions
Time complexity O (n^2), space complexity O (n).