Skip to content

Commit d90dfc2

Browse files
committed
valid-palindrome solution && complexity calc
1 parent 9e96796 commit d90dfc2

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

valid-palindrome/sungjinwi.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <string.h>
2+
#include <stdlib.h>
3+
#include <ctype.h>
4+
#include <stdbool.h>
5+
6+
/*
7+
문자열 s의 길이를 N이라고 할 때
8+
9+
시간복잡도
10+
11+
for문 두 번 돌려서 2N
12+
13+
=> N
14+
=================
15+
공간복잡도
16+
17+
두 번 복사하면서 2N
18+
19+
=> N
20+
*/
21+
22+
bool isPalindrome(char* s) {
23+
char *alnumDup = calloc(strlen(s) + 1, sizeof(char));
24+
char *revDup;
25+
int j = 0;
26+
27+
for (int i = 0; s[i]; i++)
28+
{
29+
if (isalnum(s[i]))
30+
{
31+
alnumDup[j] = tolower(s[i]);
32+
j++;
33+
}
34+
}
35+
revDup = calloc(strlen(alnumDup) + 1, sizeof(char));
36+
j = 0;
37+
for (int i = strlen(alnumDup); i; i--)
38+
revDup[j++] = alnumDup[i - 1];
39+
if (strcmp(alnumDup, revDup))
40+
{
41+
free(alnumDup);
42+
free(revDup);
43+
return (false);
44+
}
45+
else
46+
{
47+
free(alnumDup);
48+
free(revDup);
49+
return (true);
50+
}
51+
}

0 commit comments

Comments
 (0)