Skip to content

Commit a0915ac

Browse files
Create 5. Longest Palindromic Substring.cpp
1 parent 9e1b29d commit a0915ac

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

5. Longest Palindromic Substring.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
private:
3+
bool palidrom(string &s, int i , int j){
4+
if(i >= j ){
5+
return true;
6+
}
7+
if(s[i] == s[j])
8+
return palidrom(s,i+1,j-1);
9+
10+
return false;
11+
}
12+
public:
13+
string longestPalindrome(string s) {
14+
int n = s.size();
15+
if(n <= 1) return s;
16+
int maxlen = 0, st = -1;
17+
for(int i = 0 ; i < n ; i++){
18+
for(int j = i ; j < n ; j++){
19+
if(palidrom(s,i,j) == true){
20+
if(j - i + 1 > maxlen ){
21+
maxlen = j - i +1;
22+
st = i ;
23+
}
24+
}
25+
}
26+
}
27+
return s.substr(st , maxlen);
28+
}
29+
};

0 commit comments

Comments
 (0)