Skip to content

Commit a0ec37f

Browse files
fix: lowercase all better than lowercase each
1 parent 3e77e09 commit a0ec37f

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

valid-palindrome/changchanghwang.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
// O(n) space complexity
66
func isPalindrome(s string) bool {
77
regex, _ := regexp.Compile("[a-zA-Z0-9]")
8+
9+
lowerCaseString := strings.ToLower(s)
810
reverseAndFilteredString := ""
911
filteredString := ""
1012

11-
for _, char := range s {
13+
for _, char := range lowerCaseString {
1214
if regex.MatchString(string(char)) {
13-
c := strings.ToLower(string(char))
1415
reverseAndFilteredString = c + reverseAndFilteredString
1516
filteredString += c
1617
}
@@ -22,12 +23,12 @@ func isPalindrome(s string) bool {
2223
// O(n) time complexity
2324
// O(n) space complexity
2425
func isPalindrome2(s string) bool {
26+
lowerCaseString := strings.ToLower(s)
2527
reverseAndFilteredString := ""
2628
filteredString := ""
2729

28-
for _, char := range s {
30+
for _, char := range lowerCaseString {
2931
if unicode.IsLetter(char) || unicode.IsDigit(char) {
30-
c := strings.ToLower(string(char))
3132
reverseAndFilteredString = c + reverseAndFilteredString
3233
filteredString += c
3334
}

0 commit comments

Comments
 (0)