Skip to content

Commit 5e71b63

Browse files
Adding 125-Valid-Palindrome.c
1 parent a8953ea commit 5e71b63

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

c/125-Valid-Palindrome.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Time: O(n)
3+
Space: O(1)
4+
*/
5+
6+
bool isPalindrome(char * s){
7+
int n = strlen(s);
8+
if (n==1) return true;
9+
10+
int left = 0;
11+
int right = n-1;
12+
13+
while (left < right) {
14+
if (isalnum(s[left]) && isalnum(s[right])) {
15+
char leftChar = tolower(s[left]);
16+
char rightChar = tolower(s[right]);
17+
18+
if (leftChar != rightChar) {
19+
return false;
20+
}
21+
left++;
22+
right--;
23+
}
24+
25+
if (!isalnum(s[left])) left++;
26+
if (!isalnum(s[right])) right--;
27+
}
28+
29+
return true;
30+
}

0 commit comments

Comments
 (0)