Skip to content

Commit b397ccc

Browse files
committed
refactor Valid Palindrome
1 parent 4bfd891 commit b397ccc

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

go/valid_palindrome.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@ package main
44
import "unicode"
55

66
func isPalindrome(s string) bool {
7-
sRunes := []rune(s)
8-
for i, j := 0, len(s)-1; i < j; {
9-
if !(unicode.IsDigit(sRunes[i]) || unicode.IsLetter(sRunes[i])) {
10-
i++
7+
runeS := []rune(s)
8+
left, right := 0, len(s)-1
9+
for left < right {
10+
if !(unicode.IsDigit(runeS[left]) || unicode.IsLetter(runeS[left])) {
11+
left++
1112
continue
1213
}
13-
if !(unicode.IsDigit(sRunes[j]) || unicode.IsLetter(sRunes[j])) {
14-
j--
14+
if !(unicode.IsDigit(runeS[right]) || unicode.IsLetter(runeS[right])) {
15+
right--
1516
continue
1617
}
17-
if unicode.ToLower(sRunes[i]) != unicode.ToLower(sRunes[j]) {
18+
if unicode.ToLower(runeS[left]) != unicode.ToLower(runeS[right]) {
1819
return false
1920
}
20-
i++
21-
j--
21+
left++
22+
right--
2223
}
2324
return true
2425
}

0 commit comments

Comments
 (0)