Stones on the Table

Practice this coding question on Coding Gurukul.

Problem statement

An exhibition team arranges red, green and blue stones in a straight display tray. The designer wants each neighbouring pair to use different colours. Moving stones into a new order is not allowed because their original positions form part of the exhibit. Staff may only remove stones, after which the remaining stones close the gaps while keeping their relative order. Your task Given the colour sequence, determine the minimum number of stones that must be removed so no two adjacent remaining stones share a colour. R, G and B represent the three colours. Choose removals globally; you are not asked to print the final arrangement. Important details A run of identical stones may keep one representative. Stones separated by another retained colour need not be removed. A single stone already satisfies the display rule. Worked example For RRGGB, removing one R and one G leaves RGB. Two removals are necessary because both repeated-colour runs contain an extra stone.

Input format

First line: n. Second line: a string of n characters.

Output format

Print the minimum number of removals.

Constraints

1 <= n <= 50; each character is R, G or B.

Sample input

5
RRGGB

Sample output

2

Sign in to run or submit a solution in C++, Java or Python.