-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongest_Increasing_Substring-DP.cpp
More file actions
53 lines (46 loc) · 1.18 KB
/
Copy pathLongest_Increasing_Substring-DP.cpp
File metadata and controls
53 lines (46 loc) · 1.18 KB
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
#include <bits/stdc++.h>
using namespace std;
/*
Time Complexity : O(n^2)
Space Complexity : O(n^n)
*/
int LPString(string s)
{
int n = s.size();
int dp[n][n] = {0};
int len = 0;
for(int gap=0 ; gap<s.length() ; gap++)
{
for(int i=0, j=gap ; j<n ; i++, j++)
{
//we have only one option to choose when gap is 1
//as the character itself is the LPS, so put 1
if(gap == 0)
dp[i][j] = 1;
//for the gap is 2 we have two options
//if both characters are equal then true, else false
else if(gap == 1)
dp[i][j] = s[i] == s[j] ? 1 : 0;
//for the cases in which gap is more than 1
else
{
if((s[i]==s[j]) && (dp[i+1][j-1]==1))
dp[i][j] = 1;
else
dp[i][j] = 0;
}
//updating longest length
if(dp[i][j])
len = gap + 1 ;
}
}
return len;
}
int main()
{
string s;
cout<<"Enter the string : ";
cin>>s;
cout<<"Longest Palindromic Substring is of length : "<<LPString(s)<<endl;
return 0;
}