Using two pointer to solution:
- Remove non english and number character.
- Using two pointer point string start and end, move to the middle, only need to move string
length/2
step, Because if the palindrome is an odd number, the middlemost character does not affect the palindrome result. aabaa
andaaaa
,length/2
is 2,b
does not affect whether it constitutes a palindrome.
Advance problem 5. Longest Palindromic Substring.
func isPalindrome(s string) bool {
charConver(&s)
for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 {
if s[i] != s[j] {
return false
}
}
return true
}
func charConver(s *string) {
runes := []rune(*s)
var slow int
for fast := 0; fast < len(runes); fast++ {
if runes[fast] >= 65 && runes[fast] <= 90 {
runes[fast] = runes[fast] + 32
}
if runes[fast] >= 97 && runes[fast] <= 122 || runes[fast] >= 48 && runes[fast] <= 57 {
runes[slow] = runes[fast]
slow++
}
}
*s = string(runes[:slow])
}