Reputation: 1406
I am trying to solve Longest palindromic substring on Leetcode. I a aware of solutions for this problem like expand around center or dynamic programming bottom up approach. For purely educational purposes I wanted to solve this in top down recursive manner. I am trying to find solution similar to what is described here or here. (problem is slighly different). I have this function:
private (int Start, int End) Longest(string s, int i, int j)
which takes string + start and end position of search. Tuple which is return is start and end of longest palindrom. I am trying to split into these cases:
Of course I am using Dictionary with tuple (int, int) as key (values of i and j) to remember all computed results to not compute them again.
Full code is below but it is very messy after few several iterations when I was trying to fix algorithm. I believe concreate code is not very important.
Code seems to be returning correct results but fail on Time Limit Exceeded on Leetcode. Is there correct fast recursive solution? I believe there should be as DP bottom up solution exists.
code:
private readonly IDictionary<(int, int), (int, int)> _mem = new Dictionary<(int, int), (int, int)>();
private (int Start, int End) Longest(string s, int i, int j) {
if (i >= j) {
return (i, j);
}
if (_mem.TryGetValue((i, j), out var ret)) {
return ret;
}
var newI = i + 1;
var newJ = j - 1;
ValueTuple<int, int> removingTwo;
if (s[i] == s[j])
{
removingTwo = Longest(s, newI, newJ);
if (removingTwo.Item1 == newI && removingTwo.Item2 == newJ) {
removingTwo.Item1--;
removingTwo.Item2++;
}
}
else {
removingTwo = (1, 0);
}
var removingFirst = Longest(s, newI, j);
var removingLast = Longest(s, i, newJ);
var mT = removingTwo.Item2 - removingTwo.Item1;
var mF = removingFirst.End - removingFirst.Start;
var mL = removingLast.End - removingLast.Start;
var max = Math.Max(mT, mF);
max = Math.Max(max, mL);
ValueTuple<int, int> retVal;
if (max == mT) retVal = removingTwo;
else if (max == mF) retVal = removingFirst;
else retVal = removingLast;
_mem.Add((i, j), retVal);
return retVal;
}
Edit: working bottom-up solution (copied from geegsforgeegs):
public string LongestPalindrome(string s) {
if (s.Length == 0) return "";
var table = new bool[s.Length, s.Length];
var len = s.Length;
for (int i = 0; i < len; i++) {
table[i,i] = true;
}
var start = 0;
var max = 1;
for (int i = 0; i < len - 1; i++) {
if (s[i] == s[i + 1]) {
start = i;
max = 2;
table[i, i+1] = true;
}
}
for (int k = 3; k <= len; ++k) {
// Fix the starting index
for (int i = 0; i < len - k + 1; ++i)
{
// Get the ending index of substring from
// starting index i and length k
int j = i + k - 1;
// checking for sub-string from ith index to
// jth index iff str.charAt(i+1) to
// str.charAt(j-1) is a palindrome
if (table[i + 1, j - 1] && s[i] == s[j]) {
table[i,j] = true;
if (k > max) {
start = i;
max = k;
}
}
}
}
return s.Substring(start, max);
}
Upvotes: 2
Views: 2112
Reputation: 1
The best solution to find longest possible palindrome in a string using Python
inp=input("Enter a string:\n")
tmp=''
pal=[]
for i in inp:
tmp+=i
if tmp==tmp[::-1] and len(tmp)>1:
pal.append(tmp)
tmp=''
print(pal)
if len(pal)>0:
tmp=sorted(pal,key=len)
print(f'Longest palindrome : {tmp[-1]}')
else:
print("No pattern found except individual letters!")
Upvotes: 0
Reputation: 547
simple recursive solution, Not Memoization
def palandrom(A,i,j):
if i > j:
return ''
elif i == j:
return A[i]
elif A[i] == A[j]:
save = A[i+1 : j]
if save == save[::-1]:
i = len(A) #As we know rest of string are palandrome we want to make i>j condition true
return (A[i] + save + A[j])
left = palandrom(text,i+1,j)
right = palandrom(text,j,j+1)
return left if len(left) > len(right) else right
print(palandrom(loiol,0,4))
Upvotes: 0
Reputation: 23955
Here's a recursive method in Python that passes the LeetCode test. It may be that they are looking for a constant space solution.
f(i, k)
returns (l, j)
, the largest tuple of length l
and its starting index, j
. max
in this instance is looking at the first element of the returned tuple, which is l
, the palindrome's length.
def longestPalindrome(self, s):
def f(i, k):
return max(
# next iteration
f(i - 1, 1) if k < 2 and i > 0 else (-1,-1),
f(i - 1, 2) if k < 2 and i > 0 and s[i-1] == s[i] else (-1, -1),
# a larger palindrome than this one
f(i - 1, k + 2) if i > 0 and i + k < len(s) and s[i-1] == s[i + k] else (-1, -1),
# this one
(k, i)
)
(l, j) = f(len(s) - 1, 1)
return s[j:j+l]
Upvotes: 2