public static void Main()
TestLongestPalindrome("cbbd", "bb");
public static void TestLongestPalindrome(string s, string expected)
var actual = LongestPalindrome(s);
Console.WriteLine($"{s} --> {actual} expected: {expected}");
public static string LongestPalindrome(string s) {
string longestPalindrome = "";
for(int i = 0; i<s.Length; i++){
Console.WriteLine($"Begin EVEN i: {i}");
int MaxLengthEven = FindLongestPalindromeWithCenter(i, i+1,s);
Console.WriteLine($"Begin ODD i: {i}");
int MaxLengthOdd = FindLongestPalindromeWithCenter(i,i,s);
int currentMax = Math.Max(MaxLengthEven,MaxLengthOdd);
if(currentMax > maxLength){
var startIndex = i - (currentMax-1)/2;
longestPalindrome = s.Substring(startIndex,currentMax);
return longestPalindrome;
public static int FindLongestPalindromeWithCenter(int left, int right, string s){
if (right >= s.Length || s[left] != s[right])
Console.WriteLine($"Left: {s[left]}, Right: {s[right]}");
while(left > 0 && right < s.Length - 1 && s[left] == s[right]){