-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3223-MinimumLengthOfStringAfterOperations.go
More file actions
85 lines (73 loc) · 2.56 KB
/
3223-MinimumLengthOfStringAfterOperations.go
File metadata and controls
85 lines (73 loc) · 2.56 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
83
84
85
package main
// 3223. Minimum Length of String After Operations
// You are given a string s.
// You can perform the following process on s any number of times:
// 1. Choose an index i in the string such that there is at least one character to the left of index i that is equal to s[i],
// and at least one character to the right that is also equal to s[i].
// 2. Delete the closest character to the left of index i that is equal to s[i].
// 3. Delete the closest character to the right of index i that is equal to s[i].
// Return the minimum length of the final string s that you can achieve.
// Example 1:
// Input: s = "abaacbcbb"
// Output: 5
// Explanation:
// We do the following operations:
// Choose index 2, then remove the characters at indices 0 and 3. The resulting string is s = "bacbcbb".
// Choose index 3, then remove the characters at indices 0 and 5. The resulting string is s = "acbcb".
// Example 2:
// Input: s = "aa"
// Output:
// Explanation:
// We cannot perform any operations, so we return the length of the original string.
// Constraints:
// 1 <= s.length <= 2 * 10^5
// s consists only of lowercase English letters.
import "fmt"
func minimumLength(s string) int {
res, count := 0, [26]int{}
for _, v := range s {
count[v - 'a']++
}
for _, v := range count {
if v > 0 {
if v & 1 == 1 {
res++
} else {
res += 2
}
}
}
return res
}
func minimumLength1(s string) int {
res, count := 0, [26]int{}
for _, v := range s {
count[v - 'a']++
}
for _, v := range count {
res += (v - 1) % 2 + 1
}
return res
}
func main() {
// Example 1:
// Input: s = "abaacbcbb"
// Output: 5
// Explanation:
// We do the following operations:
// Choose index 2, then remove the characters at indices 0 and 3. The resulting string is s = "bacbcbb".
// Choose index 3, then remove the characters at indices 0 and 5. The resulting string is s = "acbcb".
fmt.Println(minimumLength("abaacbcbb")) // 5
// Example 2:
// Input: s = "aa"
// Output: 2
// Explanation:
// We cannot perform any operations, so we return the length of the original string.
fmt.Println(minimumLength("aa")) // 2
fmt.Println(minimumLength("bluefrog")) // 8
fmt.Println(minimumLength("leetcode")) // 6
fmt.Println(minimumLength1("abaacbcbb")) // 5
fmt.Println(minimumLength1("aa")) // 2
fmt.Println(minimumLength1("bluefrog")) // 8
fmt.Println(minimumLength1("leetcode")) // 6
}