-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2330-ValidPalindromeIV.go
More file actions
66 lines (57 loc) · 2.19 KB
/
2330-ValidPalindromeIV.go
File metadata and controls
66 lines (57 loc) · 2.19 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
package main
// 2330. Valid Palindrome IV
// You are given a 0-indexed string s consisting of only lowercase English letters.
// In one operation, you can change any character of s to any other character.
// Return true if you can make s a palindrome after performing exactly one or two operations, or return false otherwise.
// Example 1:
// Input: s = "abcdba"
// Output: true
// Explanation: One way to make s a palindrome using 1 operation is:
// - Change s[2] to 'd'. Now, s = "abddba".
// One operation could be performed to make s a palindrome so return true.
// Example 2:
// Input: s = "aa"
// Output: true
// Explanation: One way to make s a palindrome using 2 operations is:
// - Change s[0] to 'b'. Now, s = "ba".
// - Change s[1] to 'b'. Now, s = "bb".
// Two operations could be performed to make s a palindrome so return true.
// Example 3:
// Input: s = "abcdef"
// Output: false
// Explanation: It is not possible to make s a palindrome using one or two operations so return false.
// Constraints:
// 1 <= s.length <= 10^5
// s consists only of lowercase English letters.
import "fmt"
func makePalindrome(s string) bool {
count, n := 0, len(s)
for i := 0; i < n / 2; i++ {
if s[i] != s[n - i - 1] {
count++
}
}
return count <= 2 // 不能超过2个以上
}
func main() {
// Example 1:
// Input: s = "abcdba"
// Output: true
// Explanation: One way to make s a palindrome using 1 operation is:
// - Change s[2] to 'd'. Now, s = "abddba".
// One operation could be performed to make s a palindrome so return true.
fmt.Println(makePalindrome("abcdba")) // true
// Example 2:
// Input: s = "aa"
// Output: true
// Explanation: One way to make s a palindrome using 2 operations is:
// - Change s[0] to 'b'. Now, s = "ba".
// - Change s[1] to 'b'. Now, s = "bb".
// Two operations could be performed to make s a palindrome so return true.
fmt.Println(makePalindrome("aa")) // true
// Example 3:
// Input: s = "abcdef"
// Output: false
// Explanation: It is not possible to make s a palindrome using one or two operations so return false.
fmt.Println(makePalindrome("abcdef")) // false
}