-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution1221.go
More file actions
39 lines (34 loc) · 843 Bytes
/
solution1221.go
File metadata and controls
39 lines (34 loc) · 843 Bytes
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
package solution1221
// ============================================================================
// 1221. Split a String in Balanced Strings
// URL: https://leetcode.com/problems/split-a-string-in-balanced-strings/
// ============================================================================
/*
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/1221---Split-a-String-in-Balanced-Strings
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_cellsInRange-24 331438161 3.608 ns/op 0 B/op 0 allocs/op
PASS
*/
func balancedStringSplit(s string) int {
ans := 0
freqL := 0
freqR := 0
for _, char := range s {
ch := byte(char)
switch ch {
case 'L':
freqL++
case 'R':
freqR++
}
if freqL == freqR {
ans++
freqL = 0
freqR = 0
}
}
return ans
}