Mid From PDF Coding C# Coding Interview

Find the Longest Palindromic Substring?

(Repeated here for convenience)

string LongestPalindrome(string s)
{
if (string.IsNullOrEmpty(s)) return "";
int start = 0, maxLen = 1;
for (int i = 0; i < s.Length; i++)
{

ExpandAroundCenter(s, i, i, ref start, ref maxLen);

ExpandAroundCenter(s, i, i + 1, ref start, ref maxLen);

}
return s.Substring(start, maxLen);
}

void ExpandAroundCenter(string s, int left, int right, ref int

start, ref int maxLen)

{

while (left >= 0 && right < s.Length && s[left] == s[right])

{

Follow on:

if (right - left + 1 > maxLen)
{
start = left;
maxLen = right - left + 1;
}

left--;

right++;

}
}

More from C# Programming Tutorial

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details