Skip to content

Latest commit

 

History

History
39 lines (34 loc) · 1.18 KB

125.valid-palindrome.md

File metadata and controls

39 lines (34 loc) · 1.18 KB

Using two pointer to solution:

  1. Remove non english and number character.
  2. 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.
  3. aabaa and aaaa, 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])
}