Skip to content

Commit fb2c7a3

Browse files
committed
Runtime: 168 ms (Top 52.00%) | Memory: 173.7 MB (Top 19.81%)
1 parent 1b99b13 commit fb2c7a3

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1+
// Runtime: 168 ms (Top 52.00%) | Memory: 173.7 MB (Top 19.81%)
12
class Solution {
23
public:
34
string longestPalindrome(string s) {
45
if(s.size() <= 1) return s;
5-
6+
67
string longest = "";
7-
8+
89
for (int i = 0; i < s.size(); i++) {
910
string sub1 = expand(s, i, i+1);
1011
string sub2 = expand(s, i, i);
11-
12+
1213
string sub3 = sub1.size() > sub2.size() ? sub1 : sub2;
13-
14+
1415
if(sub3.size() > longest.size()) {
1516
longest = sub3;
1617
}
1718
}
1819
return longest;
1920
}
20-
21+
2122
string expand(string s, int i, int j) {
2223
while(j < s.size() && i >= 0 && s[i] == s[j]) {
2324
i--;
@@ -26,4 +27,4 @@ class Solution {
2627
// Add 1 to i and subtract 1 from j because the range is expanded by 1 on each side before it ends
2728
return s.substr(i+1, j-i-1);
2829
}
29-
};
30+
};

0 commit comments

Comments
 (0)