#K36392. Counting Consecutive Green Traffic Light Sequences
Counting Consecutive Green Traffic Light Sequences
Counting Consecutive Green Traffic Light Sequences
You are given a sequence of traffic lights consisting of characters 'G' for green and 'R' for red. Your task is to count the number of consecutive sequences (or groups) of green lights. A consecutive green sequence is defined as one or more contiguous green lights where the sequence either starts at the beginning of the string or follows a red light.
Formally, if we represent the traffic lights as a string \( S = s_1 s_2 \dots s_n \), then the answer is the number of indices \( i \) such that \( s_i = 'G' \) and either \( i = 1 \) or \( s_{i-1} \neq 'G' \).
For example, given the input "RGGGRRGG" with \( n=8 \), there are two groups of consecutive green lights: one from positions 2 to 4 and another from positions 7 to 8, so the output is 2.
inputFormat
The input consists of two lines:
- The first line contains a single integer \( n \) representing the number of traffic lights.
- The second line contains a string of length \( n \) where each character is either 'G' (green) or 'R' (red), representing the sequence of traffic lights.
outputFormat
Output a single integer representing the number of consecutive sequences of green lights.
## sample8
RGGGRRGG
2