BlackAndWhiteSolitaire
SRM 581 · 2012-12-13 · by gojira_tc
SRM 581 · 2012-12-13 · by gojira_tc · Brute Force, Simple Search, Iteration
Problem Statement
Problem Statement
Manao has N cards arranged in a sequence. He numbered them from left to right with numbers from 0 to N-1. Each card is colored black on one side and white on the other. Initially, each of the cards may lie on a different side. That is, some of the cards (possibly none or all of them) will be black side up and others will be white side up. Manao wants to flip some cards over to obtain an alternating configuration: every pair of successive cards must be of different colors.
You are given aString cardFront consisting of N characters. For each i, character i of cardFront is 'B' if card i lies black side up, and 'W' otherwise. Count and return the minimum number of cards which must be flipped to obtain an alternating configuration.
You are given a
Constraints
- cardFront will be between 3 and 50 characters long, inclusive.
- Each character in cardFront will be either 'B' or 'W'.
Examples
0)
"BBBW" Returns: 1
The first three cards lie with their black side up and the fourth card lies with its white side up. Flipping the second card will give us the alternating configuration "BWBW".
1)
"WBWBW" Returns: 0
The cards already form an alternating configuration.
2)
"WWWWWWWWW" Returns: 4
Manao only needs to flip 4 cards to make the alternating configuration "WBWBWBWBW".
3)
"BBWBWWBWBWWBBBWBWBWBBWBBW" Returns: 10
4)
"BBW" Returns: 1
Submissions are judged against all 93 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class BlackAndWhiteSolitaire with a public method int minimumTurns(string cardFront) · 93 test cases · 2 s / 256 MB per case