-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3121-CountTheNumberOfSpecialCharactersII.go
More file actions
148 lines (132 loc) · 3.93 KB
/
3121-CountTheNumberOfSpecialCharactersII.go
File metadata and controls
148 lines (132 loc) · 3.93 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
// 3121. Count the Number of Special Characters II
// You are given a string word.
// A letter c is called special if it appears both in lowercase and uppercase in word,
// and every lowercase occurrence of c appears before the first uppercase occurrence of c.
// Return the number of special letters in word.
// Example 1:
// Input: word = "aaAbcBC"
// Output: 3
// Explanation:
// The special characters are 'a', 'b', and 'c'.
// Example 2:
// Input: word = "abc"
// Output: 0
// Explanation:
// There are no special characters in word.
// Example 3:
// Input: word = "AbBCab"
// Output: 0
// Explanation:
// There are no special characters in word.
// Constraints:
// 1 <= word.length <= 2 * 10^5
// word consists of only lowercase and uppercase English letters
import "fmt"
import "math/bits"
func numberOfSpecialChars1(word string) int {
res, mp := 0, make(map[byte]int)
for i := range word {
if word[i] >= 'A' && word[i] <= 'Z' {
if _, ok := mp[word[i]]; !ok { // 只记录第一个出现的位置
mp[word[i]] = i
}
} else {
mp[word[i]] = i
}
}
for i := 0; i < 26; i++ {
v1, ok1 := mp[byte(i + 'a')]
v2, ok2 := mp[byte(i + 'A')]
if ok1 && ok2 && v2 > v1 { // 小写必须出现在大写之前
res++
}
}
return res
}
func numberOfSpecialChars(word string) int {
res, mpLower, mpUpper := 0, make(map[rune]int), make(map[rune]int)
for i, char := range word {
if char >= 'A' && char <= 'Z' {
if _, ok := mpUpper[char]; ok { // 记录第一个大写
continue
} else {
mpUpper[char] = i
}
} else {
mpLower[char] = i
}
}
for k, v := range mpLower {
if v < mpUpper[k - 32] {
res++
}
}
return res
}
func numberOfSpecialChars2(word string) int {
res, mp := 0, [52]bool{}
for i := 0; i < len(word); i++ {
b := word[i]
if b >= 'a' && b <= 'z' {
mp[b-'a'] = !mp[b - 'a' + 26]
}
if b >= 'A' && b <= 'Z' {
mp[b - 'A' + 26] = true
}
}
for i := 0; i < 26; i++ {
if mp[i] && mp[i + 26] {
res++
}
}
return res
}
func numberOfSpecialChars3(word string) int {
lower, upper, invalid := uint(0), uint(0), uint(0)
for _, c := range word {
bit := uint(1) << (c & 31)
if c & 32 > 0 {
lower |= bit
if upper&bit > 0 {
invalid |= bit
}
} else {
upper |= bit
}
}
return bits.OnesCount(lower & upper &^ invalid)
}
func main() {
// Example 1:
// Input: word = "aaAbcBC"
// Output: 3
// Explanation:
// The special characters are 'a', 'b', and 'c'.
fmt.Println(numberOfSpecialChars("aaAbcBC")) // 3
// Example 2:
// Input: word = "abc"
// Output: 0
// Explanation:
// There are no special characters in word.
fmt.Println(numberOfSpecialChars("abc")) // 0
// Example 3:
// Input: word = "AbBCab"
// Output: 0
// Explanation:
// There are no special characters in word.
fmt.Println(numberOfSpecialChars("AbBCab")) // 0
fmt.Println(numberOfSpecialChars("cCceDC")) // 0
fmt.Println(numberOfSpecialChars1("aaAbcBC")) // 3
fmt.Println(numberOfSpecialChars1("abc")) // 0
fmt.Println(numberOfSpecialChars1("AbBCab")) // 0
fmt.Println(numberOfSpecialChars1("cCceDC")) // 0
fmt.Println(numberOfSpecialChars2("aaAbcBC")) // 3
fmt.Println(numberOfSpecialChars2("abc")) // 0
fmt.Println(numberOfSpecialChars2("AbBCab")) // 0
fmt.Println(numberOfSpecialChars2("cCceDC")) // 0
fmt.Println(numberOfSpecialChars3("aaAbcBC")) // 3
fmt.Println(numberOfSpecialChars3("abc")) // 0
fmt.Println(numberOfSpecialChars3("AbBCab")) // 0
fmt.Println(numberOfSpecialChars3("cCceDC")) // 0
}