Leetcode Note: Go - Split a String in Balanced Strings

Split a String in Balanced Strings - LeetCode
https://leetcode.com/problems/split-a-string-in-balanced-strings/

  • Go 言語で取り組んだメモ

回答

Split a String in Balanced Strings - LeetCode
https://leetcode.com/problems/split-a-string-in-balanced-strings/solutions/495016/golang-o-n-easy-to-understand/

func balancedStringSplit(s string) int {
	var stack int
	var out int
	for i := range s {
		if s[i] == 'R' {
			stack++
		} else {
			stack--
		}
		if stack == 0 {
			out++
		}
	}
	return out
}