-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode Problem 5 Longest Palindromic Substring.txt
59 lines (48 loc) · 1.59 KB
/
Leetcode Problem 5 Longest Palindromic Substring.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:
Input: "cbbd"
Output: "bb"
Hint to solve: Use central diffusion(two pointer method expanding outwards for each char in the string.)
Take care of the scenario when length of palindrome is even or odd.
public class Solution
{
public string CentralDiffusion(string s, int startIndex, int endIndex)
{
int globalMax = 0;
string result = "";
while(startIndex >= 0 && endIndex != s.Length && s[startIndex] == s[endIndex])
{
int counter = endIndex - startIndex + 1;
if(counter > globalMax)
{
globalMax = counter;
result = s.Substring(startIndex, endIndex-startIndex + 1);
}
startIndex -= 1;
endIndex += 1;
}
return result;
}
public string LongestPalindrome(string s)
{
if(s.Length <= 1)
return s;
string result = "";
for(int i = 0; i<s.Length; ++i)
{
string odd_palindrome = CentralDiffusion(s, i, i);
string even_palindrome = CentralDiffusion(s, i, i+1);
string temp_max = odd_palindrome.Length > even_palindrome.Length ? odd_palindrome : even_palindrome;
if(temp_max.Length > result.Length)
{
result = temp_max;
}
}
return result;
}
}