-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path680-ValidPalindromeII.go
More file actions
82 lines (72 loc) · 1.75 KB
/
680-ValidPalindromeII.go
File metadata and controls
82 lines (72 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
// 680. Valid Palindrome II
// Given a string s, return true if the s can be palindrome after deleting at most one character from it.
// Example 1:
// Input: s = "aba"
// Output: true
// Example 2:
// Input: s = "abca"
// Output: true
// Explanation: You could delete the character 'c'.
// Example 3:
// Input: s = "abc"
// Output: false
// Constraints:
// 1 <= s.length <= 10^5
// s consists of lowercase English letters.
import "fmt"
// func validPalindrome(s string) bool {
// odd, mp := 0, make(map[byte]int)
// for i := 0; i < len(s); i++ {
// mp[s[i]]++
// }
// for _, v := range mp {
// if v % 2 == 1 {
// odd++
// if odd > 2 {
// return false
// }
// }
// }
// return true
// }
func validPalindrome(s string) bool {
i, j := 0, len(s) - 1
validSubstring := func (s string) bool {
i,j := 0, len(s) - 1
for i < j {
if s[i] == s[j] {
i++
j--
} else {
return false
}
}
return true
}
for i < j {
if s[i] == s[j] {
i++
j--
} else {
return validSubstring(s[i+1:j+1]) || validSubstring(s[i:j-1+1])
}
}
return true
}
func main() {
// Example 1:
// Input: s = "aba"
// Output: true
fmt.Println(validPalindrome("aba")) // true
// Example 2:
// Input: s = "abca"
// Output: true
// Explanation: You could delete the character 'c'.
fmt.Println(validPalindrome("abca")) // true
// Example 3:
// Input: s = "abc"
// Output: false
fmt.Println(validPalindrome("abc")) // false
fmt.Println(validPalindrome("tebbem")) // true
}