Paint The Tiles

Nikita has a line of N tiles indexed from 0 to N1. She wants to paint them to match a color configuration, C, which is comprised of 2 colors: Red(R) and Blue(B).

In one stroke, Nikita can paint 1 or more adjacent tiles a single color. After she finishes painting, each tile i should be painted color Ci.

It should be noted that it is not allowed to apply more than 1 stroke on a tile.

Given the required color configuration, find and print the minimum number of strokes required for Nikita to paint all NN tiles.

Note: In a line of tiles, 2 tiles with the indices i and j are considered adjacent only if |ji|=1.

Input Format

The first line contains a single integer, N, denoting the number of tiles to be painted.
The second line contains a string, C, denoting the desired color configuration.

For each character Ci in C:

  • If Ci=“R”, it means the ith tile must be painted red.
  • If Ci=“B”, it means the ith tile must be painted blue.

Constraints

  • 1N1000
  • Ci{“R”, “B”}

Output Format

Print the minimum number of strokes required to paint all NN tiles in the desired color configuration.

Sample Input 0

5  
RRRRR

Sample Output 0

1

Sample Input 1

5  
RRBRR

Sample Output 1

3

Sample Input 2

5  
BRBRB

Sample Output 2

5

Explanation

Sample Case 0:

Nikita will paint all 5 consecutive tiles red in a single stroke:

Sample Case 1:

Nikita will need 3 strokes to paint all 5 tiles:

import java.util.Scanner;

public class Solution {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n = input.nextInt();

String config = input.next();

int countStroke = 1;

for(int i = 0; i < config.length()-1; ++i) {

if(config.charAt(i+1) != config.charAt(i))

++countStroke;

}

System.out.println(countStroke);

}

}

Advertisements