Skip to content

Commit 380ee0c

Browse files
feat: valid-palindrome
1 parent cafc298 commit 380ee0c

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// O(n) time complexity
2+
// O(n) space complexity
3+
func isPalindrome(s string) bool {
4+
regex, _ := regexp.Compile("[a-zA-Z0-9]")
5+
reverseAndFilteredString := ""
6+
filteredString := ""
7+
8+
for _, char := range s {
9+
if regex.MatchString(string(char)) {
10+
c := strings.ToLower(string(char))
11+
reverseAndFilteredString = c + reverseAndFilteredString
12+
filteredString += c
13+
}
14+
}
15+
16+
return reverseAndFilteredString == filteredString
17+
}
18+
19+
// O(n) time complexity
20+
// O(n) space complexity
21+
func isPalindrome2(s string) bool {
22+
reverseAndFilteredString := ""
23+
filteredString := ""
24+
25+
for _, char := range s {
26+
if unicode.IsLetter(char) || unicode.IsDigit(char) {
27+
c := strings.ToLower(string(char))
28+
reverseAndFilteredString = c + reverseAndFilteredString
29+
filteredString += c
30+
}
31+
}
32+
33+
return reverseAndFilteredString == filteredString
34+
}

0 commit comments

Comments
 (0)