forked from SR-Sunny-Raj/Hacktoberfest2021-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_Longest Palindromic Substring.cpp
81 lines (64 loc) · 1.11 KB
/
4_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
72
73
74
75
76
77
78
79
80
81
/*
Problem Statement:
Given a string s, return the longest palindromic substring in s.
Example 1:
Input: s = "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:
Input: s = "cbbd"
Output: "bb"
Example 3:
Input: s = "a"
Output: "a"
Example 4:
Input: s = "ac"
Output: "a"
Constraints:
1 <= s.length <= 1000
s consist of only digits and English letters.
*/
#include <bits/stdc++.h>
using namespace std;
string longestPalindrome(string s)
{
int n = s.size();
if (n == 0)
return "";
bool dp[n][n];
memset(dp, 0, sizeof(dp));
for (int i = 0; i < n; i++)
dp[i][i] = true;
string ans = "";
ans += s[0];
for (int i = n - 1; i >= 0; i--)
{
for (int j = i + 1; j < n; j++)
{
if (s[i] == s[j])
{
if (j - i == 1 || dp[i + 1][j - 1])
{
dp[i][j] = true;
if (ans.size() < j - i + 1)
ans = s.substr(i, j - i + 1);
}
}
}
}
return ans;
}
int main()
{
std::ios::sync_with_stdio(false);
int T;
cin >> T;
// cin.ignore(); must be there when using getline(cin, s)
while (T--)
{
string s;
cin >> s;
cout << longestPalindrome(s) << endl;
}
return 0;
}