Problem Statement
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
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
"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.
"M"
Returns: "M"
"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.
"bcccddbb"
Returns: "bbD"
b BD cd C bc ===========
"BCCCDDBB"
Returns: "BBd"
"ZZ"
Returns: "z"
"abcdefggfedcba"
Returns: "ABCDEFG"
"aaaab"
Returns: "BA"
"aaaaab"
Returns: "ab"
"XYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXY"
Returns: "XYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXY"
"abcdeexffg"
Returns: "abfgE"
"accdefghiijkkk"
Returns: "ijkFEDC"
"baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaax"
Returns: "XA"
"baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaax"
Returns: "bax"
"ababababababababababababababababababababababababab"
Returns: "ababababababababababababababababababababababababab"
"aababababababababababababababababababababababababa"
Returns: "ABABABABABABABABABABABABABABABABABABABABABABABABA"
"AAAAABBBCCDEEEFFFGGGHIJKKL"
Returns: "KLihgfedc"
"bbbddd"
Returns: "bd"
"AABBCCDDEEFFGGHHAABBCC"
Returns: "Bc"
"abcdeeffeghi"
Returns: "abcfeghi"