-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursive-palindrome.c
51 lines (45 loc) · 1.07 KB
/
recursive-palindrome.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int findMin(int a, int b){
if (a > b) return b;
else return a;
}
int findMinInserts(char *str, int first, int last){
// Base cases
if (first == last)
return 0;
if (first > last){
printf("Something went wrong...\n");
abort();
}
if (first == last - 1){
if (str[first] == str[last])
return 1;
else
return 0;
}
// Main case
if (str[first] == str[last]){
return findMinInserts(str, first + 1, last - 1);
}
else {
return findMin(
findMinInserts(str, first, last - 1),
findMinInserts(str, first + 1, last)
) + 1;
}
}
int main(int argc, char **argv){
if (argc != 2){
printf("Usage: %s inputString\n", __FILE__);
exit(255);
}
clock_t t;
t = clock();
printf("%d\n", findMinInserts(argv[1], 0, strlen(argv[1]) - 1));
t = clock() - t;
printf("Took %f seconds\n", ((double)t) / CLOCKS_PER_SEC);
return 0;
}