forked from Sniper7sumit/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongest_Pallindromic_Substring.cpp
55 lines (54 loc) · 1.55 KB
/
Longest_Pallindromic_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
/* Longest Pallindromic Substring by Abhishek Kanaujiya.This Problem was available on Leetcode. So I would Like to Share my Solution.*/
class Solution {
public:
string longestPalindrome(string s) {
int n = s.length();
bool dp[n][n];
int len = 0;
int start_ind = 0 , end_ind = 0;
for(int g = 0 ; g < n ; g++)
{
for(int i = 0 , j = g ; j < n ; i++ , j++)
{
if(g == 0)
{
dp[i][j] = true;
}
else if(g == 1)
{
if(s[i] == s[j])
{
dp[i][j] = true;
}
else
{
dp[i][j] = false;
}
}
else
{
if(s[i] == s[j] && dp[i+1][j-1] == true)
{
dp[i][j] = true;
}
else
{
dp[i][j] = false;
}
}
if(dp[i][j])
{
if(len < g+1)
{
len = g+1;
start_ind = i;
end_ind = j;
cout<<i<<" "<<j<<endl;
// cout<<len<<" ";
}
}
}
}
return s.substr(start_ind , end_ind - start_ind +1);
}
};