Blog
InterviewTime limit1sMemory limit256 MB
Given a string of R, G, and B, find the minimum number of contiguous same-color paint operations needed to build it.
- Level
Medium5 of 10
- Topics
- Dynamic programming, String, Recursion, Brute force
- Solved
- No attempts yet
Problem
Didi runs the neighbor blog. Every morning she picks the problems she wants to solve for the day and writes a post. Every night, for each problem, she colors it blue if she solved it on her own, green if she solved it with help, and red if she could not solve it. When coloring the problems, Didi performs the following process as a single operation:
- Select any contiguous set of problems.
- Color all the selected problems the same color of her choice.

For example, to color the problems as shown above, coloring problem 1 green, problem 2 red, problems 3-4 blue, and problem 5 green in that order takes 4 operations, but coloring problems 1-5 green, problem 2 red, and problems 3-4 blue in that order takes 3 operations, which is the minimum. Since Didi attempts up to 100 problems every day, this work has started to feel tedious. She wants to carry out the process in the most efficient way possible. Write a program to help Didi find the minimum number of operations needed to color each problem the given color.
Input
The first line gives the number of problems to color, N (1 ≤ N ≤ 100).
The second line gives N characters in order with no spaces. Each character indicates which color the i-th problem must be colored: R is red, G is green, and B is blue. No other characters are given.
Output
On the first line, print the minimum number of operations Didi needs to color all the given problems the desired colors.