Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6a13b92

Browse files
longest palindrome
1 parent c4dd737 commit 6a13b92

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

‎03.Longest_palindromic_string.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
string longestPalindrome(string s) {
4+
int n = s.length();
5+
if(n<=1) return s;
6+
int start = 0, end = 0;
7+
for(int i = 0; i < n; i++)
8+
{
9+
int mid = palindrome(s,i,i); //racecar
10+
int mid2 = palindrome(s,i,i+1); //aabbaa
11+
int midMax = max(mid,mid2);
12+
if(midMax > end-start)
13+
{
14+
start = i-(midMax-1)/2;
15+
end = i+(midMax/2);
16+
}
17+
}
18+
return s.substr(start,end-start+1) ;
19+
}
20+
21+
int palindrome(string s, int l,int r)
22+
{
23+
int n = s.size();
24+
while(l >=0 && r < n && s[l] == s[r]) l--, r++;
25+
return r-l-1;
26+
}
27+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /