forked from LeetCode-in-Net/LeetCode-in-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
40 lines (37 loc) · 1.47 KB
/
Solution.cs
File metadata and controls
40 lines (37 loc) · 1.47 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 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 #Top_Interview_150_Multidimensional_DP
// #Big_O_Time_O(n)_Space_O(n) #2025_06_12_Time_7_ms_(95.82%)_Space_41.34_MB_(65.26%)
public class Solution {
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822", Justification = "LeetCode")]
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);
}
}
}