ColorfulBricks
SRM 551 · 2012-06-05 · by bloops
Problem Statement
Bindu wants to arrange all her bricks into a row. A row of bricks is nice if there is at most one pair of adjacent bricks which have different colors.
Return the number of ways in which Bindu can form a nice row, using all her bricks. (Two ways are considered identical if they correspond to the same sequence of brick colors.)
Constraints
- bricks will contain between 1 and 50 characters, inclusive.
- Each character of bricks will be an uppercase letter ('A'-'Z').
"ABAB" Returns: 2
There are six possible ways to arrange all the bricks into a row: "AABB", "ABAB", "ABBA", "BAAB", "BABA" and "BBAA". Out of these, only "AABB" and "BBAA" have at most one pair of adjacent bricks with different colors.
"AAA" Returns: 1
In this test case, all bricks have the same color. Hence, there is only one way to arrange them into a row. That row is nice, because it has no adjacent bricks of different colors.
"WXYZ" Returns: 0
Since all four bricks are of different colors, the bricks at positions 0 and 1 will have different colors. Similarly, the bricks at positions 1 and 2 will also have different colors. Hence, no matter how the bricks are arranged in a row, the row will not be nice.
"Z" Returns: 1
"LL" Returns: 1
Submissions are judged against all 128 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class ColorfulBricks with a public method int countLayouts(string bricks) · 128 test cases · 2 s / 256 MB per case