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:
- if s[i] == s[j] investigate Longest(s, i+1, j-1)
- Investigate Longest(s, i+1, j)
- Investigate Longest(s, i, j - 1)
- Return longest (max difference between returned start and end) from these three
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);
}