Statistics

Problem Statement for "MiddleCode"

Problem Statement

Hero is learning a new algorithm to encode a string. You are given a String s that consists of lowercase letters only. Your task is to simulate the algorithm described below on this string, so that Hero can see how it works.

The algorithm starts with a given input string s and an empty output string t. The execution of the algorithm consists of multiple steps. In each step, s and t are modified as follows:
  • If the length of s is odd, the middle character of s is added to the end of t, and then deleted from s.
  • If the length of s is even, the two characters in the middle of s are compared. The smaller one of them (either one in case of a tie) is added to the end of t, and then deleted from s.
If after some step the string s is empty, the algorithm terminates. The output of the algorithm is the final string t.

Return the String t that will be produced by the above algorithm for the given String s.

Definition

Class:
MiddleCode
Method:
encode
Parameters:
String
Returns:
String
Method signature:
String encode(String s)
(be sure your method is public)

Notes

  • When comparing letters, the smaller one is the one earlier in the alphabet - i.e., the character with the smaller ASCII code.

Constraints

  • s will contain between 1 and 50 characters, inclusive.
  • Each character in s will be a lowercase letter ('a'-'z').

Examples

  1. "word"

    Returns: "ordw"

    In the first step, 'o' is smaller than 'r', thus 'o' is selected. After the first step: s="wrd", t="o". After the second step: s="wd", t="or". In the third step, 'w' is greater than 'd', thus 'd' is selected. After the third step: s="w", t="ord". After the fourth step: s="", t="ordw", and the algorithm terminates.

  2. "aaaaa"

    Returns: "aaaaa"

  3. "abacaba"

    Returns: "caabbaa"

  4. "shjegr"

    Returns: "ejghrs"

  5. "adafaaaadafawafwfasdaa"

    Returns: "afawadafawafaafsadadaa"

  6. "bbaab"

    Returns: "aabbb"

  7. "bbaabaaaaa"

    Returns: "abaaaaabab"

  8. "bbaabaaaaabbbbbbbaba"

    Returns: "abababababbbabaabbab"

  9. "bbaabaaaaabbbbbbbababaabaabaabbabab"

    Returns: "abbabbbababbbabaaabaaaaabbbaaababbb"

  10. "bbaabaaaaabbbbbbbababaabaabaabbabababbbabbabbabbba"

    Returns: "aabbaaaabbababababbbabbbbbbbabababaaabababababbbab"

  11. "ggffgfffffgggggggfgfgffgffgffggfgfgfgggfggfggfgggf"

    Returns: "ffggffffggfgfgfgfgggfgggggggfgfgfgfffgfgfgfgfgggfg"

  12. "ghjfjjiihjffghggfhhggjhihhggifhggijhhjfjighiijggii"

    Returns: "hhgighijfgghghghfigjghhhgjfffjijghhiiiijjjfggjhigi"

  13. "ghjfjjiihjffghggfhhggjhihhggifhggijhhjfj"

    Returns: "gghjhhfighghghggfiffhjghgiiijjhjfhjjfhgj"

  14. "gmjfojnnhjkkgmggkhmlgjhihhglnkmlgnohmokj"

    Returns: "gljmhhikghghgmglknkkjmhlgnnnjohofmjokmgj"

  15. "phqghumeaylnlfdxfircvscxggbwkfnqduxwfnfozvsrtkjpre"

    Returns: "ggbxcwksfvcnqrdifuxxdwfflnfnloyzavesmrtuhkgjpqhrep"

  16. "phqghumeaylnlfdxfircvscxggbwkfnqduxwfnfoz"

    Returns: "vcscrixfggxbdfwklfnlnqyadeumxuwfhgnfqhopz"

  17. "phqghumeaylnlfdxfircvscxg"

    Returns: "lfndlxyafeimrcuhvgscqhxgp"

  18. "phqghumeay"

    Returns: "hugmeqahpy"

  19. "phqghu"

    Returns: "gqhhpu"

  20. "kfkjhgjejkejkkefkeiigghkjkjjkffjfgeijeihhiigjgeiff"

    Returns: "jkjkhjgkfgfiijefgkefeijkekijehhkijeigjgjghejikfffk"

  21. "fhkifikkgefffhfhhkhilikfikjikllkhgjehhhklhgjfejlji"

    Returns: "ikfjikikllilhkhkghhjefhhfhfhfkelghgkjkfiefijklhjfi"

  22. "jmlimfhelgmglkljlmheekjeilkjmjgegekimflhgkghkghlgf"

    Returns: "ilekjjkmejegehgmeljkilkmflglhmggkleghhfkgmhillgmfj"

  23. "mnnfljjofelljmklhlngllmoklmjklgfnooginljiljklejeji"

    Returns: "klmojmklllggfnlnhologkimjnlljleifljojkjlelfjenjnim"

  24. "pgiosihhlsjjfqfpogqkknghllkpmjgkphnlgionmkghhskfmr"

    Returns: "llhkgpmnjkgkkqgphonpflgqfijojnmsklghhhhisskofigmpr"

  25. "ljosouhuefoomsuthmurkfnehkrutjletqujsefelpmjokpoue"

    Returns: "hkernuftjklreumthqtujussemfoeoflepmuhjoukopsoojuel"

  26. "abba"

    Returns: "bbaa"

  27. "x"

    Returns: "x"

  28. "worde"

    Returns: "rdoew"

  29. "ab"

    Returns: "ab"

  30. "abc"

    Returns: "bac"


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: