forked from LeetCode-in-Net/LeetCode-in-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
38 lines (36 loc) · 1.33 KB
/
Solution.cs
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
namespace LeetCodeNet.G0001_0100.S0005_longest_palindromic_substring {
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
// #Data_Structure_II_Day_9_String #Algorithm_II_Day_14_Dynamic_Programming
// #Dynamic_Programming_I_Day_17 #Udemy_Strings #Big_O_Time_O(n)_Space_O(n)
// #2023_12_22_Time_61_ms_(99.50%)_Space_40_MB_(56.86%)
public class Solution {
public string LongestPalindrome(string s) {
if (s.Length == 1) return s;
int res = 0;
int l = 0;
int r = 0;
int len = s.Length;
for (int i = 0; i < len; i ++) {
for (int diff = 1; i - diff + 1 >= 0 && i + diff < len; diff ++) {
if (s[i - diff + 1] != s[i + diff]) break;
else if (res < diff * 2) {
res = diff * 2;
l = i - diff + 1;
r = i + diff;
}
}
}
for (int i = 0; i < len; i ++) {
for (int diff = 1; i - diff >= 0 && i + diff < len; diff ++) {
if (s[i - diff] != s[i + diff]) break;
else if (res < diff * 2 + 1) {
res = diff * 2 + 1;
l = i - diff;
r = i + diff;
}
}
}
return s.Substring(l, r - l + 1);
}
}
}