TopcoderArchiveVisit Topcoder
SRM25 May 2011
SRM 521

RedAndGreen

Problem statement, definition, constraints, and public examples.

Problem Statement

You have several squares arranged in a single row. Each square is currently painted red or green. You can choose any of the squares and paint it over with either color. The goal is that, after painting, every red square is further to the left than any of the green squares. We want you to do it repainting the minimum possible number of squares.

Squares are numbered from left to right. You will be given the initial arrangement as a String row, such that character i is 'R' if square i is red or 'G' if square i is green. Return the minimum number of repaints needed to achieve the goal.

Definition

Class:
RedAndGreen
Method:
minPaints
Parameters:
String
Returns:
int
Method signature:
int minPaints(String row)
(be sure your method is public)

Constraints

  • row will contain between 1 and 50 characters, inclusive.
  • Each character of row will be either 'R' or 'G'.

Examples

  1. "RGRGR"
    Returns: 2
    Paint the squares in the marked positions in the picture below with the opposite color. There are other ways with 2 total paints. RGRGR | | RRRGG
  2. "RRRGGGGG"
    Returns: 0
    There is no need to paint anything.
  3. "GGGGRRR"
    Returns: 3
    Paint all the red squares green.
  4. "RGRGRGRGRGRGRGRGR"
    Returns: 8
  5. "RRRGGGRGGGRGGRRRGGRRRGR"
    Returns: 9
Back to all problems