forked from TheAlgorithms/C
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Palindromic substring using dynamic programming
- Loading branch information
1 parent
031ae5f
commit e5adce3
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
/* Author : Saurav Dubey */ | ||
|
||
|
||
int countSubstrings(char* s) { | ||
int len = strlen(s); | ||
int i; | ||
int count = 0; | ||
for( i=0; i<len; i++){ | ||
// cases handled for both odd and even lenghted Palindrome | ||
|
||
count += countPalin(s, i, i, len); | ||
if(i!=len-1) | ||
count += countPalin(s, i, i+1, len); | ||
} | ||
return count; | ||
} | ||
int countPalin(char *s, int head, int tail, int len){ | ||
int ret = (s[head]==s[tail])? 1:0 ; | ||
if( ret && head-1>=0 && tail+1<len) | ||
ret += countPalin(s, head-1, tail+1, len); | ||
return ret; | ||
} |