#K74692. Minimum Removals for Forbidden Substrings
Minimum Removals for Forbidden Substrings
Minimum Removals for Forbidden Substrings
Given a string S consisting solely of the characters R
, G
, and B
, your task is to determine the minimum number of characters that must be removed so that no forbidden adjacent pairs remain.
A forbidden adjacent pair is defined as follows:
- If a character
R
is immediately followed by eitherG
orB
. - If a character
G
is immediately followed by eitherR
orB
. - If a character
B
is immediately followed by eitherR
orG
.
Whenever a forbidden pair is encountered, one removal is counted and the next character in the pair is skipped. The goal is to achieve a string in which every pair of consecutive characters does not form any of the above forbidden combinations.
Example: For the input string RGBG
, the minimal number of removals is 2.
inputFormat
The input consists of a single string S on one line, where S contains only the characters 'R', 'G', and 'B'.
outputFormat
Output a single integer representing the minimum number of removals required to eliminate all forbidden adjacent pairs.
## sampleRGBG
2