-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathE125_Valid_Palindrome.c
92 lines (78 loc) · 2.06 KB
/
E125_Valid_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#include<stdbool.h>
void normalization(char *input, char *output, unsigned int * cnt )
{
char *tmp = input;
while( *tmp != '\0') {
if( *tmp >= 'A' && *tmp <= 'Z') {
*output = 'a' + (*tmp - 'A');
output++;
(*cnt)++;
} else if( ( *tmp >= 'a' && *tmp <= 'z' ) ||
( *tmp >= '0' && *tmp <= '9' ) ) {
*output = *tmp;
output++;
(*cnt)++;
}
tmp++;
}
*output = '\0';
}
bool isPalindrome( char *s ) {
unsigned long length = strlen( s );
char *normalstr = NULL;
unsigned int i;
unsigned int * nrml_cnt = malloc( sizeof(unsigned int));
bool ret = true;
*nrml_cnt = 0;
if( length > 0 ) {
normalstr = malloc( length * sizeof(char) );
} else {
// If empty, we consider the str as a palindrome
return true;
}
normalization( s, normalstr, nrml_cnt );
printf("normalstr = %s, nrml_cnt = %d \n", normalstr, *nrml_cnt );
for( i = 0; i < *nrml_cnt / 2; i++ ) {
if( *( normalstr + i ) != *(normalstr + *nrml_cnt - i - 1 ) ) {
ret = false;
break;
}
}
return ret;
}
/*************************************************
* Version 2 is from leetcode solution, write here
* just for backup.
*************************************************/
bool isPalindrome2(char* s) {
char *head, *tail;
int len = strlen(s);
if( len <= 0 ) return true;
head = s;
tail = s + len - 1;
while(head < tail) {
if( !isalnum( *head ) ) { head ++; continue;}
if( !isalnum( *tail ) ) { tail --; continue;}
if( tolower(*head++) != tolower(*tail--)) { return false;}
}
return true;
}
int main()
{
char *test = "abcdbba";
if( isPalindrome( test ) ) {
printf("True!\n");
} else {
printf("False!\n");
}
if( isPalindrome2( test ) ) {
printf("True!\n");
} else {
printf("False!\n");
}
return 0;
}