aboutsummaryrefslogtreecommitdiff
path: root/valid_palindrome.c
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-02-27 12:00:57 -0700
committerJack Sangdahl <[email protected]>2025-02-27 12:00:57 -0700
commita1bd06c84b438b48a6c01fb1b14d74571752cf50 (patch)
tree86333d78cbc96e17d5c45ee38dfddc0c2c7c4000 /valid_palindrome.c
parentd9f08bf1f55bbdd365c8204ad5261a66f7d8fc25 (diff)
downloadleetcode-a1bd06c84b438b48a6c01fb1b14d74571752cf50.tar.gz
leetcode-a1bd06c84b438b48a6c01fb1b14d74571752cf50.zip
125. valid palindrome
Diffstat (limited to 'valid_palindrome.c')
-rw-r--r--valid_palindrome.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/valid_palindrome.c b/valid_palindrome.c
new file mode 100644
index 0000000..d3dfe81
--- /dev/null
+++ b/valid_palindrome.c
@@ -0,0 +1,35 @@
+// 125. Valid Palindrome
+#include "leetcode.h"
+
+/*
+ * a phrase is a palindrome if, after converting all uppercase letters into
+ * lowercase letters, and removing all non-alphanumeric characters, it reads the
+ * same forward and backward. alphanumeric characters include only letters and
+ * numbers. given a string 's', return true if it is a palindrome, or false
+ * otherwise.
+ */
+
+bool isPalindrome(char *s) {
+ int l = 0, r = strlen(s) - 1;
+ while (l < r) {
+ if (isalnum(s[l])) {
+ if (isalnum(s[r])) {
+ if (tolower(s[l]) != tolower(s[r]))
+ return false;
+ l++, r--;
+ } else
+ r--;
+ } else
+ l++;
+ }
+ return true;
+}
+
+int main() {
+ char *s1 = "A man, a plan, a canal: Panama";
+ char *s2 = "race a car";
+ char *s3 = " ";
+ printf("%d\n", isPalindrome(s1)); // expect: 1
+ printf("%d\n", isPalindrome(s2)); // expect: 0
+ printf("%d\n", isPalindrome(s3)); // expect: 1
+}