forked from Viv786ek/Problem-Of-The-Day
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Longest Palindromic Substring .cpp
71 lines (48 loc) · 1.64 KB
/
Longest Palindromic Substring .cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
string longestPalindrome(string s){
// code here
vector<vector<bool>> string_map(s.size(), vector<bool>(s.size(),false));
string ans = "";
int str_len = 0;
int i = 0;
while(i < s.size()){
string_map[i][i] = true;
int curr_len = 1;
if(curr_len > str_len){
str_len = curr_len;
ans = s.substr(i, str_len);
}
i++;
}
i=0;
while(i<s.size()-1){
if(s[i] == s[i+1]){
string_map[i][i+1] = true;
int curr_len = 2;
if(curr_len > str_len){
str_len = curr_len;
ans = s.substr(i,str_len);
}
}
i++;
}
int k = 2;
int j = k;
while(k<s.size()){
i=0;
j=k;
while(i<s.size()-k){
if(s[i] == s[j] && string_map[i+1][j-1]){
string_map[i][j] = true;
int curr_len = k+1;
if(curr_len > str_len){
str_len = curr_len;
ans = s.substr(i, str_len);
}
}
i++;
j++;
}
k++;
}
return ans;
}