-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathPalindromic Substrings.cpp
57 lines (42 loc) · 1.38 KB
/
Palindromic Substrings.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
/*
Solution by Rahul Surana
***********************************************************
Given a string s, return the number of palindromic substrings in it.
A string is a palindrome when it reads the same backward as forward.
A substring is a contiguous sequence of characters within the string.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
int countSubstrings(string s) {
int dp[1002][1002];
memset(dp,0,sizeof(dp));
string t = s;
// reverse(t.begin(),t.end());
int ans = 0;
for(int i = s.length()-1; i >= 0; i--){
for(int j = i; j < t.length(); j++){
if(i==j){
ans++;
dp[i][j]=1;
}
else if(s[i]==t[j]){
if(abs(j-i) == 1){
dp[i][j] = 1;
ans++;
}
else{
ans+= dp[i+1][j-1];
if(dp[i+1][j-1]) dp[i][j] = 1;
}
}
else{
dp[i][j] = 0;
}
// cout << i << " "<<j <<" "<< ans <<"\n";
}
}
return ans;
}
};