Statistics

Problem Statement for "KthStringAgain"

Problem Statement

You are given a String s of length m. We will now define a collection of exactly 2^m (not necessarily distinct) strings. The collection is generated by the following pseudocode:
start with an empty collection
for each subset X of the set {1,2,...,m}:
    take a new string t and initialize it to the given string s
    for i = 1,2,...,m:
        if X contains i:
            reverse the last i characters of t
    add the string t to the collection
You are also given a long k. Imagine that we sorted our entire collection lexicographically. Find and return the string that will appear k-th in the sorted collection. (Note that k is a 1-based index.)

Definition

Class:
KthStringAgain
Method:
getKth
Parameters:
String, long
Returns:
String
Method signature:
String getKth(String s, long k)
(be sure your method is public)

Constraints

  • s will consist only of lowercase English letters ('a'-'z').
  • The length of s will be between 1 and 50, inclusive.
  • k will be between 1 and 2^n, inclusive, where n is the length of s.

Examples

  1. "xyz"

    5

    Returns: "yzx"

    Pseudocode will generate the following strings: "xyz" for X = {} (empty set), "xyz" for X = {1}, "xzy" for X = {2}, "xzy" for X = {1, 2}, "zyx" for X = {3}, "zyx" for X = {1, 3}, "yzx" for X = {2, 3}, "yzx" for X = {1, 2, 3}. Sorted lexicographically collection: "xyz", "xyz", "xzy", "xzy", "yzx", "yzx", "zyx", "zyx". The 5th string is "yzx".

  2. "abc"

    1

    Returns: "abc"

  3. "abc"

    8

    Returns: "cba"

  4. "topcoder"

    58

    Returns: "ooredcpt"

  5. "w"

    2

    Returns: "w"

  6. "rtcjbjlbtjfmvzdqutuw"

    137644

    Returns: "cjlbjmvdutwuqzftjbtr"

  7. "wobervhvvkihcuyjtmqhaaigvahheoqleromusrartldojsjvy"

    38517901796974

    Returns: "bervviaigahqlerrloyvjsjdtarsumooehvahqmtjyuchkvhow"

  8. "xiwpcfsmzenbvpdffpnduqiihivouiobhyfyiwng"

    700745285105

    Returns: "xipfsdfpnuqivuioyfiwgnyhboihidfpvbnezmcw"

  9. "udotcwvcwxajkadxqvxvwgmwmnqrby"

    893855030

    Returns: "uotvaxvmnqybrwmgwvxqdakjxwcwcd"

  10. "aaaabcdeeeghhhhhiijjjkllmmooooqqrrrrssttuuvvvvvwyy"

    38517901796974

    Returns: "aaaacdeehhhijklmmqqrsttvvvvwyyvuusrrrooooljjihhgeb"

  11. "bbcddeefffggghhiijjjkkmnnnoqqstttuuvvwwzzzzzz"

    28230392110897

    Returns: "cdeffgghhiijjkotuvwzzzzzzwvuttsqqnnnmkjgfedbb"

  12. "bbcdfjjjlmqrtttuuvwz"

    137644

    Returns: "bbdfjjlmttuvwzutrqjc"

  13. "koq"

    5

    Returns: "oqk"

  14. "zyyyyxxxwwvvuuttsqqqpppppoooomlkkkkkiiiihhhgfffcbb"

    650961305794880

    Returns: "zyxwvvusqppppoolkkiihhhbbcfffgiikkkmoopqqttuwxxyyy"

  15. "zzzyxxwwvvuuttttsssrqqqppppponllljhhggggffcccbbaa"

    180989502964001

    Returns: "zyvvtsssqqppnjggfcbaabccfgghhlllopppqrtttuuwwxxzz"

  16. "zzyyxxwwvvvvvuuuutttsssrrqqppommjjiiihgfeeccbbba"

    77202987521863

    Returns: "zwwvuutsssrqpjiigecbbabcefhijmmopqrttuuvvvvxxyyz"

  17. "bc"

    2

    Returns: "bc"

  18. "yh"

    4

    Returns: "yh"

  19. "aaacdddefghhkkllmnnooqqqqrrtuvxxzz"

    11726632515

    Returns: "acghkklnooqqqrrtuxxzzvqnmlhfedddaa"

  20. "abbccdffiijjlnppqrrsttwwxzz"

    133333547

    Returns: "filppqrrttwxzzwsnjjifdccbba"

  21. "yxsrrqonmkhgddcba"

    43594

    Returns: "xrqomgcabddhknrsy"

  22. "pndsgmfchgmfbnpudilllsdlinwjtwhxurhpmouxoyylttrcyj"

    1109036256392552

    Returns: "smcgmfdllsupmytryjctlyoxuohrxhwtjwnildliupnbhfgdnp"

  23. "jultlnqgvwwbgbatseeofkrzjrbvrdolxgkrcgirvugrtmehvs"

    1125843229998239

    Returns: "wwgeokrrvrxrgirugtmevshrvckglodbjzfestabbvgqnltluj"

  24. "iigphdowhosnulizcgoz"

    1048356

    Returns: "wsulozgczinohodhpgii"

  25. "vpernozjkgggbneohsinmloogugatfewx"

    8589922432

    Returns: "zsmlooutxwefaggnihoenbgggkjonrepv"

  26. "zyzeelknqumwcsulxyxsjhagogsceqqvlbfcvdmxgia"

    8792059282029

    Returns: "zzuwcsxxsaggcqldxaigmvcfbvqesohjylumqnkleey"

  27. "kmbyltiycqfbfjxmbywkpiaoforswtgwrjjcvbhhceun"

    2851211260339

    Returns: "kblticfjykiaofoswgwjvhheuncbcjrtrpwbmxbfqyym"

  28. "yofbeazaxjawujfplnmrdnyxswiezvroxqcfwkhqwt"

    1

    Returns: "aaacfhqtwkwqxorvzeiwsxyndrmnlpfjuwjxzebfoy"

  29. "mwjottptqdqfvxonealajqkheft"

    1

    Returns: "aaefthkqjlenoxvfqdqtpttojwm"

  30. "jvxljtolmgjndlwoyjjttakhzvzmihjdhkyfn"

    28757114701

    Returns: "jvljoyjjtahzzmihfnykdjhvktwldngmotjlx"

  31. "mluuxpwzhgaadltqqpqkbteylathdyvzcowrtrdmplgbpl"

    3379746181201

    Returns: "lupwzaqqpqyathyoplgplbmdrtrwczvdletbktldaghxum"

  32. "bejrrlbcintahovgjdd"

    211074

    Returns: "blbntahogddjvicrrje"

  33. "lmffiacftelleszdckdtwiivawxdv"

    178075928

    Returns: "lffafeletiawvdxviwdkcdzsltcim"

  34. "obervhvvkihcuyjtmqhaaigvahheoql"

    1465836743

    Returns: "oerkihujqvaholqehgiaahmtycvvhvb"

  35. "qvuqz"

    18

    Returns: "qzuvq"

  36. "bppzfsncq"

    147

    Returns: "bpsqcnfzp"

  37. "okalgqjhyijyizyihdsiappbgvzxzevykavqm"

    135945714359

    Returns: "qjhyijizdsipvzxzevykavqmgbpahiyyglako"

  38. "lfpbavjsmppdtlkfwnyfmbdhptdswsoulrbhyjh"

    223124182042

    Returns: "lbavjsppdlknmdhptdwsubyhjhrlosbfywftmpf"

  39. "ehjjkmqrty"

    1019

    Returns: "rytqmkjjhe"

  40. "abbccccdeeeffhhhikmmnnopppqqrrrrsssstuuvvxxxxxyyyz"

    1125898988520404

    Returns: "nnppqqrrssstuvvxxyzyyxxxusrrpommkihhhffeeedccccbba"

  41. "aacdilrstwxx"

    3686

    Returns: "distxxwrlcaa"

  42. "abcdeeefffffklooptttuuwxxxxzz"

    1

    Returns: "abcdeeefffffklooptttuuwxxxxzz"

  43. "zzzzyyxvvttsqqpooonnmmllkkjhhgffeeddbbaa"

    1099499808588

    Returns: "zzzzyyxvvttsqqpoonmkgfeeddbbaafhhjkllmno"

  44. "zzyxwwvvusrqpooonlkjiifcb"

    1

    Returns: "bcfiijklnooopqrsuvvwwxyzz"

  45. "zwwvtspommkiihc"

    32443

    Returns: "zwwvtsomiichkmp"

  46. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

    999999999999

    Returns: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

  47. "asdfasdfasd"

    1234

    Returns: "dfasddsafsa"


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: