Skip to content

Commit 32266ff

Browse files
Create Find all distinct palindromic substrings of a given string.cpp
1 parent b2befbc commit 32266ff

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
Find all distinct palindromic substrings of a given string
3+
Send Feedback
4+
Ninja did not do homework. As a penalty, the teacher has given a string ‘S’ to ninja and asked him to print all distinct palindromic substrings of the given string. A string is said to be palindrome if the reverse of the string is the same as the string itself. For example, the string “bccb” is a palindrome, but the string “def” is not a palindrome.
5+
Input Format:
6+
The first line of input contains an integer ‘T’, denoting the number of test cases.
7+
The first and only line of each test case contains the string ‘S’.
8+
Output Format:
9+
For each test case, print all distinct palindromic substrings of the given string. Print the substrings in sorted manner and they should be space-separated.
10+
11+
Print the output of each test case in a separate line.
12+
Constraints:
13+
1 <= T <= 10
14+
1 <= |S| <= 1000
15+
16+
String S contains lowercase English letters only.
17+
18+
Where ‘T’ represents the number of test cases and ‘S’ represents the given string.
19+
20+
Time limit: 1 sec
21+
Sample Input 1:
22+
2
23+
aba
24+
aabb
25+
Sample Output 1:
26+
a aba b
27+
a aa b bb
28+
Explanation of sample input 1:
29+
For the first test case,
30+
All the possible substrings are [ ‘a’, ‘b’, ‘ab’, ‘ba’, ‘aba’ ] out of which [ ‘a’, ‘aba', 'b’ ] are
31+
palindromic substrings.
32+
33+
For the second test case,
34+
All the possible substrings are [ ‘a’, ‘b’, ‘aa’, ‘ab’, ‘bb’, ‘aab’, ‘abb’ , ‘aabb’ ] out of which [
35+
‘a’, ‘b’, ‘aa’, ‘bb’ ] are palindromic substrings.
36+
Sample Input 2:
37+
2
38+
babbb
39+
abcdc
40+
Sample Output 2:
41+
a b bab bb bbb
42+
a b c cdc d
43+
*/
44+
45+
46+
47+
#include<bits/stdc++.h>
48+
49+
vector<string>distinctPalindrome(string &s)
50+
{
51+
int n = s.size();
52+
53+
vector<vector<bool> > dp(n, vector<bool>(n, false));
54+
for (int i = 0; i < n; i++)
55+
{
56+
dp[i][i] = 1;
57+
58+
if (i < n && s[i] == s[i + 1]) {
59+
dp[i][i + 1] = 1;
60+
}
61+
}
62+
63+
for (int len = 3; len <= n; len++)
64+
{
65+
for (int i = 0; i + len - 1 < n; i++)
66+
{
67+
if (s[i] == s[i + (len - 1)] && dp[i + 1][i + (len - 1) - 1])
68+
{
69+
dp[i][i + (len - 1)] = true;
70+
}
71+
}
72+
}
73+
74+
vector<int> kmp(n, 0);
75+
76+
for (int i = 0; i < n; i++)
77+
{
78+
int j = 0, k = 1;
79+
while (k + i < n)
80+
{
81+
if (s[j + i] == s[k + i])
82+
{
83+
dp[k + i - j][k + i] = false;
84+
kmp[k++] = ++j;
85+
}
86+
87+
else if (j > 0)
88+
{
89+
j = kmp[j - 1];
90+
}
91+
92+
else
93+
{
94+
kmp[k++] = 0;
95+
}
96+
}
97+
}
98+
99+
vector<string>ans;
100+
101+
for (int i = 0; i < n; i++)
102+
{
103+
string str;
104+
for (int j = i; j < n; j++)
105+
{
106+
str += s[j];
107+
if (dp[i][j])
108+
{
109+
ans.push_back(str);
110+
}
111+
}
112+
}
113+
114+
sort(ans.begin(),ans.end());
115+
116+
return ans;
117+
}

0 commit comments

Comments
 (0)