IdealString
SRM 405 · 2008-06-14 · by Petr
Problem Statement
An ideal string is a string where the 1-based index of the first occurrence of each letter is equal to the number of occurrences of that letter in the string. For example, the "BAOOOA" is an ideal string (quotes for clarity only). The letter 'B' appears 1 time, and its index is 1. The letter 'A' occurs 2 times and its first index is 2. The letter 'O' occurs 3 times and its first index is 3.
Given an
Notes
- String A is lexicographically smaller than string B of the same length if it contains a smaller letter at the first position they differ. Letter X is smaller than letter Y if it comes earlier in the alphabet.
Constraints
- length will be between 1 and 100, inclusive.
1 Returns: "A"
3 Returns: "ABB"
2 Returns: ""
There's no way we can construct an ideal string of length 2 - if both its characters are equal, then the number of occurrences (2) and the position of its first occurrence (1) do not match; if they are distinct, then for the second character the number of occurrences (1) and the position of its first occurrence (2) do not match as well.
4 Returns: ""
6 Returns: "ABCBCC"
We can permute the last 3 characters in any way, but this way gets us the lexicographically smallest answer.
Submissions are judged against all 100 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class IdealString with a public method string construct(int length) · 100 test cases · 2 s / 256 MB per case