generated from eyamenko/dotnet-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem55.cs
More file actions
40 lines (35 loc) · 1.27 KB
/
Problem55.cs
File metadata and controls
40 lines (35 loc) · 1.27 KB
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
namespace LeetCode;
/// <summary>
/// <see href="https://leetcode.com/problems/longest-palindromic-substring/">Longest Palindromic Substring</see>.
/// </summary>
public static class Problem55
{
/// <summary>
/// Given a string s, return the longest palindromic substring in s.
/// Time complexity: O(n^2).
/// Space complexity: O(1).
/// </summary>
/// <param name="s">String to traverse.</param>
/// <returns>Longest palindromic substring.</returns>
public static string LongestPalindrome(string s)
{
var (maxLeft, maxRight) = (0, 0);
for (var i = 1; i < s.Length; i++)
{
(maxLeft, maxRight) = LongestPalindromeWindow(s, i - 1, i, maxLeft, maxRight);
(maxLeft, maxRight) = LongestPalindromeWindow(s, i - 1, i + 1, maxLeft, maxRight);
}
return s[maxLeft..(maxRight + 1)];
}
private static (int MaxLeft, int MaxRight) LongestPalindromeWindow(string s, int left, int right, int maxLeft, int maxRight)
{
for (; left >= 0 && right < s.Length && s[left] == s[right]; left--, right++)
{
if (right - left > maxRight - maxLeft)
{
(maxLeft, maxRight) = (left, right);
}
}
return (maxLeft, maxRight);
}
}