Statistics

Problem Statement for "StrIIRec"

Problem Statement

For a given string S of length n an inversion is a pair of integers (i, j) such that 0 <= i < j <= n-1 and S[i] > S[j]. (That is, the character at 0-based index i is greater than the character at 0-based index j.) For example, the string "abcab" has 3 inversions: (1, 3), (2, 3), and (2, 4).

Given are ints n and minInv, and a String minStr. We will consider all strings that are permutations of the first n lowercase English letters. That is, these strings have length n and contain each of the first n letters exactly once. Out of these strings, return the lexicographically smallest string R with the following two properties:

  • The number of inversions in R is at least minInv.
  • The string R is not lexicographically smaller than minStr.
If there is no such string, return an empty String instead.

Definition

Class:
StrIIRec
Method:
recovstr
Parameters:
int, int, String
Returns:
String
Method signature:
String recovstr(int n, int minInv, String minStr)
(be sure your method is public)

Notes

  • A String A is lexicographically smaller than a String B if A is a prefix of B or A contains a smaller character at the first position where the Strings differ.

Constraints

  • n will be between 1 and 20, inclusive.
  • minInv will be between 0 and n*(n-1)/2, inclusive.
  • minStr will contain between 1 and n characters, inclusive.
  • Each character in minStr will be one of the first n lowercase Latin letters.
  • All characters in minStr will be unique.

Examples

  1. 1

    0

    "a"

    Returns: "a"

  2. 2

    0

    "a"

    Returns: "ab"

  3. 2

    0

    "b"

    Returns: "ba"

  4. 2

    1

    "a"

    Returns: "ba"

  5. 2

    1

    "b"

    Returns: "ba"

  6. 2

    0

    "ab"

    Returns: "ab"

  7. 2

    1

    "ab"

    Returns: "ba"

    You must find the lexicographically smallest String that has at least 1 inversion and is not lexicographically smaller than "ab".

  8. 2

    0

    "ba"

    Returns: "ba"

  9. 2

    1

    "ba"

    Returns: "ba"

  10. 16

    64

    "k"

    Returns: "kabcdopnmljihgfe"

  11. 9

    1

    "efcdgab"

    Returns: "efcdgabhi"

  12. 13

    77

    "ljhefmciabdgk"

    Returns: "lmkjihgfedcba"

  13. 3

    0

    "bca"

    Returns: "bca"

  14. 8

    1

    "gdbcah"

    Returns: "gdbcahef"

  15. 15

    3

    "enjkmlbhaio"

    Returns: "enjkmlbhaiocdfg"

  16. 11

    12

    "acgedif"

    Returns: "acgedifbhjk"

  17. 8

    16

    "af"

    Returns: "afdhgecb"

  18. 13

    33

    "dkmlabfghj"

    Returns: "dkmlabfghjcei"

  19. 10

    12

    "hdfjig"

    Returns: "hdfjigabce"

  20. 11

    16

    "dhiajge"

    Returns: "dhiajgebcfk"

  21. 4

    1

    "ca"

    Returns: "cabd"

  22. 3

    0

    "acb"

    Returns: "acb"

  23. 3

    1

    "c"

    Returns: "cab"

  24. 13

    32

    "fcdbh"

    Returns: "fcdbhakmljige"

  25. 18

    84

    "ifebqhljk"

    Returns: "ifebqhljkrponmgdca"

  26. 20

    124

    "rosqkcip"

    Returns: "rosqkcipabdjtnmlhgfe"

  27. 15

    105

    "jkblogm"

    Returns: "onmlkjihgfedcba"

  28. 11

    0

    "egifh"

    Returns: "egifhabcdjk"

  29. 3

    0

    "bac"

    Returns: "bac"

  30. 11

    55

    "debgikjfc"

    Returns: "kjihgfedcba"

    "kjihgfedcba" is the only String that has at least 55 inversions.

  31. 4

    4

    "cbd"

    Returns: "cbda"

  32. 11

    2

    "fhgkicb"

    Returns: "fhgkicbadej"

  33. 18

    51

    "hqkrebo"

    Returns: "hqkreboacdfgijlmnp"

  34. 13

    65

    "fcedmjigl"

    Returns: "fgmlkjihedcba"

  35. 18

    136

    "o"

    Returns: "ocrqpnmlkjihgfedba"

  36. 11

    4

    "biaf"

    Returns: "biafcdeghjk"

  37. 4

    4

    "ad"

    Returns: "bdca"

  38. 9

    10

    "b"

    Returns: "bacdhigfe"

  39. 4

    5

    "ad"

    Returns: "cdba"

  40. 5

    1

    "e"

    Returns: "eabcd"

  41. 20

    99

    "asjilotdqgcepbmnkrh"

    Returns: "asjilotdqgcepbmnrkhf"

  42. 12

    39

    "labdefcgkhij"

    Returns: "labdjkihgfec"

  43. 8

    14

    "h"

    Returns: "habdgfec"

  44. 19

    122

    "afqinebcsrmlgh"

    Returns: "afqinosrpmlkjhgedcb"

  45. 6

    12

    "bfead"

    Returns: "cfedba"

  46. 6

    7

    "bcd"

    Returns: "bcefda"

  47. 17

    16

    "e"

    Returns: "eabcdfghijknqpoml"

  48. 4

    1

    "cbd"

    Returns: "cbda"

  49. 10

    27

    "aihecjgf"

    Returns: "aihecjgfdb"

  50. 8

    22

    "fahb"

    Returns: "fchgedba"

  51. 15

    24

    "adilgme"

    Returns: "adilgmebcfhjkno"

  52. 14

    72

    "fcengdika"

    Returns: "fcmnlkjihgedba"

  53. 12

    50

    "gjeh"

    Returns: "gjehklifdcba"

  54. 19

    116

    "enhmdirfbjlosk"

    Returns: "enhmdirqspolkjgfcba"

  55. 7

    6

    "bfd"

    Returns: "bfdaceg"

  56. 9

    20

    "feh"

    Returns: "fehabigdc"

  57. 6

    12

    "dcbfe"

    Returns: "defcba"

  58. 20

    39

    "mahkidsbtpref"

    Returns: "mahkidsbtprefcgjlnoq"

  59. 8

    18

    "defhac"

    Returns: "defhcgba"

  60. 16

    15

    "ahjpk"

    Returns: "ahjpkbcdefgilmno"

  61. 18

    124

    "bqkdrflcinej"

    Returns: "bqkjrponmlihgfedca"

  62. 9

    17

    "hcedfbai"

    Returns: "hcedfbaig"

  63. 8

    28

    "gabhdcf"

    Returns: "hgfedcba"

  64. 20

    24

    "lfd"

    Returns: "lfdabceghijkmnopstrq"

  65. 17

    69

    "apqemchbldkgi"

    Returns: "apqemchbldkjonigf"

  66. 5

    4

    "a"

    Returns: "acedb"

  67. 6

    11

    "f"

    Returns: "faedcb"

  68. 19

    24

    "enbosmr"

    Returns: "enbosmracdfghijklpq"

  69. 5

    1

    "ebac"

    Returns: "ebacd"

  70. 17

    88

    "lipbn"

    Returns: "lipbnacqomkjhgfed"

  71. 13

    48

    "jlhaci"

    Returns: "jlhacidmkgfeb"

  72. 6

    11

    "abc"

    Returns: "bfedca"

  73. 8

    13

    "gcbhde"

    Returns: "gcbhdeaf"

  74. 19

    19

    "ebjcha"

    Returns: "ebjchadfgiklmnoprsq"

  75. 19

    48

    "caro"

    Returns: "carobdefghijpsqnmlk"

  76. 11

    6

    "cakib"

    Returns: "cakibdefghj"

  77. 18

    42

    "fpbkojqgenrilhdac"

    Returns: "fpbkojqgenrilhdacm"

  78. 11

    2

    "edbjaki"

    Returns: "edbjakicfgh"

  79. 8

    10

    "e"

    Returns: "eabchgfd"

  80. 9

    21

    "h"

    Returns: "habgifedc"

  81. 8

    16

    "cfh"

    Returns: "cfhaegdb"

  82. 14

    56

    "kiamjhbecld"

    Returns: "kiamjhbelngfdc"

  83. 19

    18

    "irfebmslhgadn"

    Returns: "irfebmslhgadncjkopq"

  84. 10

    18

    "aie"

    Returns: "aiebcgjhfd"

  85. 11

    43

    "hejgb"

    Returns: "hejgikfdcba"

  86. 15

    22

    "glmc"

    Returns: "glmcabdefhijkno"

  87. 4

    3

    "adcb"

    Returns: "adcb"

  88. 20

    41

    "cardgepnlbhsfk"

    Returns: "cardgepnlbhsfkijmoqt"

  89. 15

    70

    "onfmcj"

    Returns: "onfmcjabhlkiged"

  90. 9

    29

    "fadg"

    Returns: "fdihgecba"

  91. 9

    5

    "igehacbd"

    Returns: "igehacbdf"

  92. 10

    37

    "cjfbegad"

    Returns: "cjhigfedba"

  93. 13

    40

    "cbfmkjadielh"

    Returns: "cbfmkjaglihed"

  94. 13

    29

    "fmcbkaigjeld"

    Returns: "fmcbkaigjeldh"

  95. 19

    134

    "fshkpinjemqdoblagr"

    Returns: "fshkplrqonmjigedcba"

  96. 15

    0

    "e"

    Returns: "eabcdfghijklmno"

  97. 7

    20

    "d"

    Returns: "fgedcba"

  98. 18

    48

    "nbchflikdr"

    Returns: "nbchflikdraegjmopq"

  99. 9

    20

    "fcdebiha"

    Returns: "fcdehigba"

  100. 6

    2

    "bfad"

    Returns: "bfadce"

  101. 9

    19

    "ecah"

    Returns: "ecahgifdb"

  102. 12

    41

    "hbgdj"

    Returns: "hbgdjlkifeca"

  103. 12

    38

    "a"

    Returns: "abelkjihgfdc"

  104. 20

    148

    "ogrhsf"

    Returns: "ogrhsfntqpmlkjiedcba"

  105. 12

    29

    "gkc"

    Returns: "gkcabdhljife"

  106. 18

    19

    "eqncbkoflrhjma"

    Returns: "eqncbkoflrhjmadgip"

  107. 15

    82

    "ne"

    Returns: "neamolkjihgfdcb"

  108. 9

    1

    "cahfgbei"

    Returns: "cahfgbeid"

  109. 15

    61

    "jmkhnd"

    Returns: "jmkhndabciolgfe"

  110. 20

    17

    "cijnsqbephkatfdroglm"

    Returns: "cijnsqbephkatfdroglm"

  111. 20

    79

    "phjn"

    Returns: "phjnabcdefktsrqomlig"

  112. 20

    5

    "bedo"

    Returns: "bedoacfghijklmnpqrst"

  113. 20

    189

    "mikjardhcnltfqo"

    Returns: "strqponmlkjihgfedcba"

  114. 20

    0

    "eljtohsbkfid"

    Returns: "eljtohsbkfidacgmnpqr"

  115. 20

    190

    "mj"

    Returns: "tsrqponmlkjihgfedcba"

  116. 20

    156

    "pfjmqkdarlegthscobin"

    Returns: "pfjmrtsqonlkihgedcba"

  117. 20

    30

    "mnglorcifpbethjkdqas"

    Returns: "mnglorcifpbethjkdqas"

  118. 20

    93

    "r"

    Returns: "rabcdefqtsponmlkjihg"

  119. 20

    0

    "q"

    Returns: "qabcdefghijklmnoprst"

  120. 20

    0

    "mq"

    Returns: "mqabcdefghijklnoprst"

  121. 20

    184

    "filhtmkeajpobqdscgrn"

    Returns: "ntsrqpomlkjihgfedcba"

  122. 20

    187

    "c"

    Returns: "qtsrponmlkjihgfedcba"

  123. 20

    1

    "jicndftlobmhqapgsrke"

    Returns: "jicndftlobmhqapgsrke"

  124. 20

    187

    "jasghodbcmreqflin"

    Returns: "qtsrponmlkjihgfedcba"

  125. 20

    188

    "gnha"

    Returns: "rtsqponmlkjihgfedcba"

  126. 20

    188

    "ckamtdrfblhqnipeojgs"

    Returns: "rtsqponmlkjihgfedcba"

  127. 20

    84

    "k"

    Returns: "kabcdefptsrqonmljihg"

  128. 20

    0

    "itfhaqsbodlkjn"

    Returns: "itfhaqsbodlkjncegmpr"

  129. 20

    185

    "kcplegqo"

    Returns: "otsrqpnmlkjihgfedcba"

  130. 20

    0

    "rnsdjfibgclphemqaok"

    Returns: "rnsdjfibgclphemqaokt"

  131. 20

    6

    "c"

    Returns: "cabdefghijklmnoprtsq"

  132. 20

    173

    "oncijgrmhakbedts"

    Returns: "onktsrqpmljihgfedcba"

  133. 20

    3

    "qrslchogjbdnetfkaim"

    Returns: "qrslchogjbdnetfkaimp"

  134. 20

    62

    "agb"

    Returns: "agbcdefhktsrqponmlji"

  135. 20

    5

    "bkodjecamhqgrtfnpis"

    Returns: "bkodjecamhqgrtfnpisl"

  136. 20

    79

    "rhdictqojapmkl"

    Returns: "rhdictqojapmklbefgns"

  137. 20

    180

    "aomtgrknslecfibqjdhp"

    Returns: "jtsrqponmlkihgfedcba"

  138. 20

    42

    "ih"

    Returns: "ihabcdefgjklstrqponm"

  139. 20

    190

    "nbdqchgltakjipemfr"

    Returns: "tsrqponmlkjihgfedcba"

  140. 20

    189

    "scgkbqnejptfldrmhi"

    Returns: "strqponmlkjihgfedcba"

  141. 20

    175

    "sjcnitagrqfkbelphmd"

    Returns: "sjntrqpomlkihgfedcba"

  142. 20

    8

    "jcibomhegpd"

    Returns: "jcibomhegpdafklnqrst"

  143. 20

    185

    "trc"

    Returns: "trnsqpomlkjihgfedcba"

  144. 20

    190

    "dp"

    Returns: "tsrqponmlkjihgfedcba"

  145. 20

    141

    "jd"

    Returns: "jdamtsrqponlkihgfecb"

  146. 20

    1

    "ltgskpobhmfd"

    Returns: "ltgskpobhmfdaceijnqr"

  147. 20

    179

    "hkmldcgfpiobt"

    Returns: "itsrqponmlkjhgfedcba"

  148. 20

    1

    "stmopcl"

    Returns: "stmopclabdefghijknqr"

  149. 20

    2

    "idctqmfonbkjresp"

    Returns: "idctqmfonbkjrespaghl"

  150. 20

    0

    "fjpmldbcniqshkgoear"

    Returns: "fjpmldbcniqshkgoeart"

  151. 20

    5

    "lcoerk"

    Returns: "lcoerkabdfghijmnpqst"

  152. 20

    187

    "tdjsingplohmea"

    Returns: "tpsrqonmlkjihgfedcba"

  153. 20

    188

    "fsheicnod"

    Returns: "rtsqponmlkjihgfedcba"

  154. 20

    0

    "tgm"

    Returns: "tgmabcdefhijklnopqrs"

  155. 20

    150

    "brsmfhjkg"

    Returns: "brsmfotqpnlkjihgedca"

  156. 20

    1

    "dnarhjlm"

    Returns: "dnarhjlmbcefgikopqst"

  157. 20

    190

    "dcaqkr"

    Returns: "tsrqponmlkjihgfedcba"

  158. 20

    7

    "p"

    Returns: "pabcdefghijklmnoqrst"

  159. 20

    187

    "klgrcpjobnmqdea"

    Returns: "qtsrponmlkjihgfedcba"

  160. 20

    185

    "ajsdflk"

    Returns: "otsrqpnmlkjihgfedcba"

  161. 20

    123

    "k"

    Returns: "kabcmtsrqponljihgfed"

  162. 20

    190

    "a"

    Returns: "tsrqponmlkjihgfedcba"

  163. 20

    70

    "abdej"

    Returns: "abdejcfgqtsrponmlkih"

  164. 20

    100

    "a"

    Returns: "abcdeotsrqpnmlkjihgf"

  165. 20

    20

    "a"

    Returns: "abcdefghijklmstrqpon"

  166. 11

    55

    "kjihgfedcba"

    Returns: "kjihgfedcba"

  167. 20

    130

    "a"

    Returns: "abcntsrqpomlkjihgfed"

  168. 20

    31

    "abcdqefghijklmnop"

    Returns: "abcdqefghijklrtsponm"

  169. 12

    56

    "debgikjfcl"

    Returns: "djlkihgfecba"

  170. 20

    34

    "abcdefghi"

    Returns: "abcdefghijkrtsqponml"

  171. 20

    180

    "abcdmnopqrstefghijkl"

    Returns: "jtsrqponmlkihgfedcba"

  172. 20

    176

    "tip"

    Returns: "tiprsqonmlkjhgfedcba"

  173. 20

    179

    "mbjcalhtepsfonkqgdir"

    Returns: "mptsrqonlkjihgfedcba"

  174. 20

    190

    "abcdefghijklmnopqrst"

    Returns: "tsrqponmlkjihgfedcba"

  175. 20

    150

    "abcdefghijklmnopqrst"

    Returns: "abqtsrponmlkjihgfedc"

  176. 20

    100

    "dcaefog"

    Returns: "dcaefoltsrqpnmkjihgb"

  177. 20

    100

    "abcdefg"

    Returns: "abcdeotsrqpnmlkjihgf"

  178. 20

    130

    "tqcd"

    Returns: "tqcdabsrponmlkjihgfe"

  179. 20

    171

    "a"

    Returns: "atsrqponmlkjihgfedcb"

  180. 20

    190

    "abcdehjkmnpoqrstf"

    Returns: "tsrqponmlkjihgfedcba"

  181. 15

    0

    "abcdefghijklmno"

    Returns: "abcdefghijklmno"

  182. 20

    189

    "onm"

    Returns: "strqponmlkjihgfedcba"

  183. 20

    3

    "a"

    Returns: "abcdefghijklmnopqtsr"

  184. 20

    190

    "s"

    Returns: "tsrqponmlkjihgfedcba"

  185. 20

    190

    "tsrqponmlkjihgfedcba"

    Returns: "tsrqponmlkjihgfedcba"

  186. 5

    3

    "abdec"

    Returns: "abedc"

  187. 19

    20

    "fcdebihamnk"

    Returns: "fcdebihamnkgjlopqrs"

  188. 20

    90

    "a"

    Returns: "abcdefstrqponmlkjihg"

  189. 20

    95

    "a"

    Returns: "abcdejtsrqponmlkihgf"

  190. 20

    190

    "bacdefghijklmnopqrst"

    Returns: "tsrqponmlkjihgfedcba"

  191. 4

    3

    "abcd"

    Returns: "adcb"

  192. 3

    0

    "abc"

    Returns: "abc"

  193. 20

    50

    "a"

    Returns: "abcdefghiotsrqpnmlkj"

  194. 20

    190

    "abcdefghijklmnop"

    Returns: "tsrqponmlkjihgfedcba"

  195. 3

    1

    "bac"

    Returns: "bac"

  196. 15

    88

    "nbcdefghklmo"

    Returns: "nbjomlkihgfedca"

  197. 19

    20

    "fcdebiha"

    Returns: "fcdebihagjklmnopsrq"

  198. 4

    3

    "a"

    Returns: "adcb"

  199. 9

    10

    "fcdebiha"

    Returns: "fcdebihag"

  200. 16

    50

    "ib"

    Returns: "ibacdelponmkjhgf"

  201. 6

    8

    "a"

    Returns: "adfecb"

  202. 19

    151

    "b"

    Returns: "bpsrqonmlkjihgfedca"

  203. 20

    66

    "abcdefghijklmnopqrst"

    Returns: "abcdefghtsrqponmlkji"

  204. 20

    171

    "bacdefghijklmnopqrst"

    Returns: "bstrqponmlkjihgfedca"

  205. 20

    180

    "a"

    Returns: "jtsrqponmlkihgfedcba"

  206. 20

    129

    "a"

    Returns: "abcmtsrqponlkjihgfed"


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: