forked from aishwarydewangan/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlongest_palindromic_substring.cpp
60 lines (44 loc) · 1.11 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
/*
Given a string, find the length of its Longest Palindromic Substring (LPS). In a palindromic
string, elements read the same backward and forward.
Example 1:
Input: "abdbca"
Output: 3
Explanation: LPS is "bdb".
Example 2:
Input: = "cddpd"
Output: 3
Explanation: LPS is "dpd".
Example 3:
Input: = "pqr"
Output: 1
Explanation: LPS could be "p", "q" or "r".
*/
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> memo;
int lps(string& input, int l, int r) {
if(l > r) {
return 0;
}
if(l == r) {
return 1;
}
if(memo[l][r] != -1) {
return memo[l][r];
}
if(input[l] == input[r]) {
int length = r-l-1;
if(length == lps(input, l+1, r-1)) {
return (memo[l][r] = length+2);
}
}
return (memo[l][r] = max(lps(input, l, r-1), lps(input, l+1, r)));
}
int main() {
string input = "abdbca";
int n = input.size();
memo.resize(n, vector<int>(n, -1));
cout << "Length of Longest Palindromic Substring: " << lps(input, 0, n-1);
return 0;
}