Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Nov 26, 2023
1 parent 38c9e33 commit f4ece02
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions leetcode/680.ValidPalindromeII/validPalindromeII.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"fmt"
)

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

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

return true
}

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

func main() {
s := "abca"
fmt.Println(validPalindrome(s))
}

0 comments on commit f4ece02

Please sign in to comment.