#K68512. Minimum Switches Between Actions
Minimum Switches Between Actions
Minimum Switches Between Actions
You are given a sequence of actions performed by a robot. Each character in the sequence is either P
(for picking an object) or L
(for placing an object). The task is to compute the minimum number of switches between the two actions. A switch is defined as a change from one action to the other (i.e. from P
to L
or from L
to P
).
For example, if the input sequence is PPPLLPPPLL
, the switches occur between the groups of characters as follows:
from PPP
to LL
(1 switch), from LL
to PP
(2 switches), and from PP
to LL
(3 switches), resulting in a total of 3 switches.
If the sequence is empty or has only one type of character, the answer is 0. Mathematically, if we denote the sequence by \( s = s_1s_2 \ldots s_n \), then the answer can be computed as:
[ \text{switches}(s) = \sum_{i=2}^{n} \mathbf{1}{{s_i \neq s{i-1}}}, ]
where \( \mathbf{1}_{\{\cdot\}} \) is the indicator function.
inputFormat
The input consists of a single line, which is a string representing the sequence of actions. The string only contains the characters P
and L
and may be empty.
outputFormat
Output a single integer representing the number of switches between picking and placing actions.
## samplePPPLLPPPLL
3