mirror of
https://github.com/TheAlgorithms/C.git
synced 2026-09-24 16:05:02 +00:00
647.c
Palindromic substring using dynamic programming
This commit is contained in:
committed by
GitHub
parent
031ae5f1d2
commit
e5adce3a52
23
leetcode/src/647.c
Normal file
23
leetcode/src/647.c
Normal file
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user