Skip to content

Commit 910bced

Browse files
committed
647.cpp
1 parent ff50dbc commit 910bced

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

647.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
int countSubstrings(string s) {
4+
5+
int n=s.length();
6+
int count=0;
7+
int dp[n][n];
8+
for(int i=0;i<n;i++){
9+
for(int j=0;j<n;j++){
10+
dp[i][j]=0;
11+
}
12+
}
13+
for(int i=0;i<n;i++){
14+
dp[i][i]=1;
15+
count++;
16+
}
17+
for(int i=1;i<n;i++){
18+
for(int j=0;j<i;j++){
19+
if(i-1==j && s[i]==s[j]){
20+
dp[j][i]=1;
21+
count++;
22+
}
23+
else if(s[i]==s[j] && dp[j+1][i-1]==1){
24+
dp[j][i]=1;
25+
count++;
26+
}
27+
}
28+
}
29+
return count;
30+
31+
32+
}
33+
};

0 commit comments

Comments
 (0)