Content-Length: 308885 | pFad | http://github.com/JanaSabuj/Leetcode-solutions/commit/b967b4fc57016fcb23811ccc5b072af50ab44080

0D Time: 19 ms (91.58%), Space: 6.9 MB (99.67%) - LeetHub · JanaSabuj/Leetcode-solutions@b967b4f · GitHub
Skip to content

Commit b967b4f

Browse files
committed
Time: 19 ms (91.58%), Space: 6.9 MB (99.67%) - LeetHub
1 parent 8fbb174 commit b967b4f

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public:
3+
pair<int,int> checkPal(int i, int j, int n, string& str) {
4+
while(i >= 0 and j < n and str[i] == str[j]) {
5+
i--,j++;
6+
}
7+
i++,j--;
8+
9+
return {i,j};
10+
// sz = max(sz, j - i + 1);
11+
}
12+
13+
string longestPalindrome(string str) {
14+
int n = str.size();
15+
int centers = 2*n - 1;
16+
// * * * * * * *
17+
// 0 1 2 3
18+
// c
19+
int sz = 0;
20+
int l = -1, r = -1;
21+
for(int c = 0; c < n; c++) {
22+
// int i = c - 1, j = c + 1;
23+
pair<int,int> p1 = checkPal(c-1,c+1,n, str);
24+
pair<int, int> p2 = checkPal(c,c+1,n, str);
25+
if((p1.second - p1.first + 1) >= sz) {
26+
sz = (p1.second - p1.first + 1);
27+
l = p1.first;
28+
r = p1.second;
29+
}
30+
31+
if((p2.second - p2.first + 1) >= sz) {
32+
sz = (p2.second - p2.first + 1);
33+
l = p2.first;
34+
r = p2.second;
35+
}
36+
}
37+
38+
return str.substr(l, sz);
39+
}
40+
};

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/JanaSabuj/Leetcode-solutions/commit/b967b4fc57016fcb23811ccc5b072af50ab44080

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy