Statistics

Problem Statement for "StringFold"

Problem Statement

I have a strip of paper with a string of characters on it. On one side it is in uppercase, and on the other lowercase. (If you turned the paper over, making the left end become the right end, you would see the string in reverse order, and in the opposite case).

We put the strip on the table with the leftmost letter glued down. The free end is originally extending to the right. We work our way toward the free end, checking for identical adjacent letters. If we find an adjacent pair, we apply glue to them and fold the paper to glue them together. That leaves the strip with its free end extending in the opposite direction and with its opposite side exposed. We continue with this procedure until we reach the free end.

Create a class StringFold that contains the method foldString that takes a String, original, as input and returns a String that shows the result of our folding and gluing.

Definition

Class:
StringFold
Method:
foldString
Parameters:
String
Returns:
String
Method signature:
String foldString(String original)
(be sure your method is public)

Notes

  • The total number of folds made will always equal the number of adjacent pairs of the same letter in original.
  • The return should show just the exposed letters.
  • Only pairs of adjacent identical letters in original are folded together; it is possible that the returned string may have identical adjacent letters (see example 2).

Constraints

  • original contains between 1 and 50 characters, inclusive.
  • Each character in original is a letter.
  • The case of each letter in original is the same (either all uppercase, or all lowercase).

Examples

  1. "ABBCD"

    Returns: "dcb"

    dcb ABBCD AB ======== ======== This shows side views with the edge of the table at the bottom. The left picture shows the original position of the string with the 'A' glued to the table. The right picture shows the final view after the two adjacent 'B's are folded together. From above, only the "dcb" are visible.

  2. "M"

    Returns: "M"

  3. "acccdee"

    Returns: "acdE"

    E cdee cde EEDCC C C acccdee ac ac ac ========= ========= ======== ========= This shows four pictures of the string as it is being folded. The leftmost picture shows the initial situation after the a is glued to the table. The second picture shows the situation after the first fold. After the next fold, the third picture shows that the free end is once again extending to the right with the lowercase side up. The last diagram shows the final configuration. "acdE" are the letters that are now visible.

  4. "bcccddbb"

    Returns: "bbD"

    b BD cd C bc ===========

  5. "BCCCDDBB"

    Returns: "BBd"

  6. "ZZ"

    Returns: "z"

  7. "abcdefggfedcba"

    Returns: "ABCDEFG"

  8. "aaaab"

    Returns: "BA"

  9. "aaaaab"

    Returns: "ab"

  10. "XYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXY"

    Returns: "XYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXY"

  11. "abcdeexffg"

    Returns: "abfgE"

  12. "accdefghiijkkk"

    Returns: "ijkFEDC"

  13. "baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaax"

    Returns: "XA"

  14. "baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaax"

    Returns: "bax"

  15. "ababababababababababababababababababababababababab"

    Returns: "ababababababababababababababababababababababababab"

  16. "aababababababababababababababababababababababababa"

    Returns: "ABABABABABABABABABABABABABABABABABABABABABABABABA"

  17. "AAAAABBBCCDEEEFFFGGGHIJKKL"

    Returns: "KLihgfedc"

  18. "bbbddd"

    Returns: "bd"

  19. "AABBCCDDEEFFGGHHAABBCC"

    Returns: "Bc"

  20. "abcdeeffeghi"

    Returns: "abcfeghi"


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: