给定一个字符串S ,任务是找到长度最长的回文子串
例子:
Input: S = “aaaabbaa”
Output: 6
Explanation:
Sub-string “aabbaa” is the longest palindromic sub-string.
Input: S = “banana”
Output: 5
Explanation:
Sub-string “anana” is the longest palindromic sub-string.
方法:想法是使用递归将问题分解为更小的子问题。为了将问题分解为两个较小的子问题,比较字符串的开始和结束字符,并递归调用中间子字符串的函数。下面是递归的图示:
- 基本情况:此问题的基本情况是字符串的起始索引大于或等于结束索引。
if (start > end) return count if (start == end) return count + 1
- 递归案例:比较字符串的开始和结束索引字符:
- 当开始和结束字符相等时,通过排除开始和结束字符递归调用子字符串
recursive_func(string, start+1, end-1)
- 当开始和结束字符不相等时,通过一次排除一个开始和结束字符递归调用子字符串。
recursive_func(string, start+1, end) recursive_func(string, start, end-1)
- 当开始和结束字符相等时,通过排除开始和结束字符递归调用子字符串
- 返回语句:在每次递归调用时,通过包括和排除开始和结束字符返回可能的最大计数。
下面是上述方法的实现:
C++
// C++ implementation to find the
// length of longest palindromic
// sub-string using Recursion
#include
using namespace std;
// Function to find maximum
// of the two variables
int max(int x, int y)
{
return (x > y) ? x : y;
}
// Function to find the longest
// palindromic substring : Recursion
int longestPalindromic(string str,
int i, int j, int count)
{
// Base condition when the start
// index is greater than end index
if (i > j)
return count;
// Base condition when both the
// start and end index are equal
if (i == j)
return (count + 1);
// Condition when corner characters
// are equal in the string
if (str[i] == str[j]) {
// Recursive call to find the
// longest Palindromic string
// by excluding the corner characters
count = longestPalindromic(str, i + 1,
j - 1, count + 2);
return max(count,
max(longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0)));
}
// Recursive call to find the
// longest Palindromic string
// by including one corner
// character at a time
return max(
longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0));
}
// Function to find the longest
// palindromic sub-string
int longest_palindromic_substr(string str)
{
// Utility function call
return longestPalindromic(str, 0,
str.length() - 1, 0);
}
// Driver Code
int main()
{
string str = "aaaabbaa";
// Function Call
cout << longest_palindromic_substr(str);
return 0;
}
Java
// Java implementation to find the
// length of longest palindromic
// sub-String using Recursion
class GFG{
// Function to find maximum
// of the two variables
static int max(int x, int y)
{
return (x > y) ? x : y;
}
// Function to find the longest
// palindromic subString : Recursion
static int longestPalindromic(String str,
int i, int j, int count)
{
// Base condition when the start
// index is greater than end index
if (i > j)
return count;
// Base condition when both the
// start and end index are equal
if (i == j)
return (count + 1);
// Condition when corner characters
// are equal in the String
if (str.charAt(i) == str.charAt(j)) {
// Recursive call to find the
// longest Palindromic String
// by excluding the corner characters
count = longestPalindromic(str, i + 1,
j - 1, count + 2);
return max(count,
max(longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0)));
}
// Recursive call to find the
// longest Palindromic String
// by including one corner
// character at a time
return Math.max(
longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0));
}
// Function to find the longest
// palindromic sub-String
static int longest_palindromic_substr(String str)
{
// Utility function call
return longestPalindromic(str, 0,
str.length() - 1, 0);
}
// Driver Code
public static void main(String[] args)
{
String str = "aaaabbaa";
// Function Call
System.out.print(longest_palindromic_substr(str));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation to find the
# length of longest palindromic
# sub-string using Recursion
# Function to find maximum
# of the two variables
def maxi(x, y) :
if x > y :
return x
else :
return y
# Function to find the longest
# palindromic substring : Recursion
def longestPalindromic(strn, i, j, count):
# Base condition when the start
# index is greater than end index
if i > j :
return count
# Base condition when both the
# start and end index are equal
if i == j :
return (count + 1)
# Condition when corner characters
# are equal in the string
if strn[i] == strn[j] :
# Recursive call to find the
# longest Palindromic string
# by excluding the corner characters
count = longestPalindromic(strn, i + 1, j - 1, count + 2)
return maxi(count, maxi(longestPalindromic(strn, i + 1, j, 0),
longestPalindromic(strn, i, j - 1, 0)))
# Recursive call to find the
# longest Palindromic string
# by including one corner
# character at a time
return maxi( longestPalindromic(strn, i + 1, j, 0),
longestPalindromic(strn, i, j - 1, 0))
# Function to find the longest
# palindromic sub-string
def longest_palindromic_substr(strn):
# Utility function call
k = len(strn) - 1
return longestPalindromic(strn, 0, k, 0)
strn = "aaaabbaa"
# Function Call
print( longest_palindromic_substr(strn) )
# This code is contributed by chsadik99
C#
// C# implementation to find the
// length of longest palindromic
// sub-String using Recursion
using System;
class GFG{
// Function to find maximum
// of the two variables
static int max(int x, int y)
{
return (x > y) ? x : y;
}
// Function to find the longest
// palindromic subString : Recursion
static int longestPalindromic(String str,
int i, int j, int count)
{
// Base condition when the start
// index is greater than end index
if (i > j)
return count;
// Base condition when both the
// start and end index are equal
if (i == j)
return (count + 1);
// Condition when corner characters
// are equal in the String
if (str[i] == str[j]) {
// Recursive call to find the
// longest Palindromic String
// by excluding the corner characters
count = longestPalindromic(str, i + 1,
j - 1, count + 2);
return max(count,
max(longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0)));
}
// Recursive call to find the
// longest Palindromic String
// by including one corner
// character at a time
return Math.Max(
longestPalindromic(str, i + 1, j, 0),
longestPalindromic(str, i, j - 1, 0));
}
// Function to find the longest
// palindromic sub-String
static int longest_palindromic_substr(String str)
{
// Utility function call
return longestPalindromic(str, 0,
str.Length - 1, 0);
}
// Driver Code
public static void Main(String[] args)
{
String str = "aaaabbaa";
// Function Call
Console.Write(longest_palindromic_substr(str));
}
}
// This code is contributed by 29AjayKumar
输出:
6
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live