Statistics

Problem Statement for "AlphaWord"

Problem Statement

The government of a small but important country has decided that the alphabet needs to be streamlined and reordered. Uppercase letters will be eliminated. They will issue a decree in the form of a string of 'B' and 'A' characters corresponding to Before or After. The first character in the decree specifies whether 'a' must come Before 'b' in the new alphabet or After 'b'. The second character determines the relative placement of 'b' and 'c', and in general, character i of the decree specifies the relative placement in the new alphabet of characters i and i+1 from the old alphabet. So, for example, "BAAB" means that 'a' must come Before 'b', 'b' must come After 'c', 'c' must come After 'd', and 'd' must come Before 'e'.

Any letters beyond these requirements are to be excluded, so if the decree specifies k comparisons then the new alphabet will contain the first k+1 lowercase letters of the current alphabet.

Create a class Alphabet that contains the method firstAlpha that takes the decree as input and returns the new alphabet as a String that is first alphabetically among all alphabets that conform to the decree. Specifically, if 2 alphabets conform to the decree, choose the one with the lower character in the first position in which they differ.

Definition

Class:
AlphaWord
Method:
firstAlpha
Parameters:
String
Returns:
String
Method signature:
String firstAlpha(String decree)
(be sure your method is public)

Constraints

  • decree contains between 1 and 25 characters, inclusive.
  • Each character in decree is the uppercase letter 'B' or 'A'.

Examples

  1. "BAA"

    Returns: "adcb"

    'a' Before 'b', 'b' After 'c', and 'c' After 'd' have been decreed, so possibilities are "adcb", "dacb", "dcab".

  2. "AAAA"

    Returns: "edcba"

    "edcba" is the only alphabet that conforms.

  3. "BBAA"

    Returns: "abedc"

    all the alphabets that satisfy this are: "edabc", "eadbc", "aedbc", "eabdc", "aebdc", and "abedc". Of these, the first is "abedc".

  4. "BABAAA"

    Returns: "acbgfed"

  5. "A"

    Returns: "ba"

  6. "B"

    Returns: "ab"

  7. "BAAAAAAAAAAAAAAAAAAAAAAAA"

    Returns: "azyxwvutsrqponmlkjihgfedcb"

  8. "AAAAAAAAAAAAAAAAAAAAAAAAA"

    Returns: "zyxwvutsrqponmlkjihgfedcba"

  9. "BBBBBBBBBBBBBBBBBBBBBBBBB"

    Returns: "abcdefghijklmnopqrstuvwxyz"

  10. "AABBAABBAABBB"

    Returns: "cbadgfehkjilmn"

  11. "BBBBBBBBBBBBAAAAAAAAAAAA"

    Returns: "abcdefghijklyxwvutsrqponm"

  12. "BABABABABABABABABABABABAB"

    Returns: "acbedgfihkjmlonqpsrutwvyxz"

  13. "BABBAAABBBB"

    Returns: "acbdhgfeijkl"

  14. "BBBBBBBA"

    Returns: "abcdefgih"

  15. "AAAAAAB"

    Returns: "gfedcbah"

  16. "AAAAAAAAAAAAAAAAAAAAAAAAA"

    Returns: "zyxwvutsrqponmlkjihgfedcba"

  17. "ABBABAABBAABAABAABBBA"

    Returns: "bacedhgfilkjonmrqpstvu"

  18. "BBAA"

    Returns: "abedc"

  19. "BABABABABABABABABABABAB"

    Returns: "acbedgfihkjmlonqpsrutwvx"

  20. "BABABABA"

    Returns: "acbedgfih"

  21. "ABBA"

    Returns: "baced"

  22. "BABABABABABABABABABABABAB"

    Returns: "acbedgfihkjmlonqpsrutwvyxz"

  23. "ABBABABBAAABB"

    Returns: "bacedgfhlkjimn"

  24. "BBBBBBBBBBBBBBBBBBBBBBBBB"

    Returns: "abcdefghijklmnopqrstuvwxyz"

  25. "AABBA"

    Returns: "cbadfe"


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: