Skip to content

Commit fe93bfd

Browse files
authored
solution(cpp): 5. Longest Palindromic Substring
5. Longest Palindromic Substring - C++
2 parents e5ed7f3 + 5036ef6 commit fe93bfd

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
3+
class Solution {
4+
public:
5+
bool check (int i,int j , string s){
6+
while(i<=j){
7+
if(s[i] != s[j]){
8+
return false;
9+
}else{
10+
i++;
11+
j--;
12+
}
13+
}
14+
return true;
15+
}
16+
string longestPalindrome(string s) {
17+
int maxlen=0;
18+
int index =0;
19+
for(int i =0;i<s.size();i++){
20+
for(int j =i;j<s.size();j++){
21+
if(s[i]==s[j]){
22+
bool ans = check(i,j,s);
23+
if(ans){
24+
if(j-i+1 > maxlen){
25+
maxlen = j-i+1;
26+
index =i;
27+
}
28+
}
29+
}
30+
}
31+
}
32+
return s.substr(index,maxlen);
33+
}
34+
};

0 commit comments

Comments
 (0)