valid_palindrome_ii

  • 2022-12-14
  • 浏览 (490)

valid_palindrome_ii.go 源码

package string

// 验证回文字符串II

func validPalindrome(s string) bool {
	i, j := 0, len(s)-1
	for i < j {
		if s[i] != s[j] {
			return valid(s, i+1, j) || valid(s, i, j-1)
		} else {
			i, j = i+1, j-1
		}
	}
	return true
}

func valid(s string, i, j int) bool {
	for i < j {
		if s[i] == s[j] {
			i, j = i+1, j-1
		} else {
			return false
		}
	}
	return true
}

你可能感兴趣的文章

brute_force

brute_force_test

isomorphic_strings

0  赞