File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments