Connection Status:
Competition Arena > ThreeProgrammers
2016 TCO Algo 1C · 2016-03-24 · by praveen123 · Simple Math, Simple Search, Iteration
Class Name: ThreeProgrammers
Return Type: String
Method Name: validCodeHistory
Arg Types: (string)
Problem Statement

Problem Statement

This problem is about three programmers who work together on a project. The programmers' names are Alice, Bob, and Charles. We will use 'A', 'B', and 'C' to denote them.

Each day exactly one of the three programmers works on the project. The code history is a string that specifies who worked on which day. For example, the string "AAC" means that Alice worked on the project for two days in a row and then Charles worked for a day.

Alice is always able to work on the project. Each time Bob spends a day working, he needs to take at least one day off before he is able to work again. Each time Charles spends a day working, he needs to take at least two days off.

The above information means that not all strings are valid code histories. For example, the string "BB" is not a valid code history because Bob cannot work two days in a row.

You are given a String code. This may or may not be a valid code history. Find any permutation of letters of code that produces a valid code history, and return that code history. If there are multiple solutions, you may return any of them. If there are no solutions, return "impossible" instead.

Constraints

  • code will contain between 1 and 50 characters, inclusive.
  • Each character in code will be one of 'A', 'B', and 'C'.
Examples
0)
"CAB"
Returns: "BCA"

The input is a valid code history. In fact, any permutation of this input is a valid code history, and you may return any of them.

1)
"CBB"
Returns: "BCB"

Bob cannot work on two consecutive days. Hence, "BCB" is the only valid code history that is a permutation of the given input.

2)
"BB"
Returns: "impossible"
3)
"BBA"
Returns: "BAB"
4)
"CAC"
Returns: "impossible"

Submissions are judged against all 200 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class ThreeProgrammers with a public method string validCodeHistory(string code) · 200 test cases · 2 s / 256 MB per case

Submitting as anonymous