Statistics

Problem Statement for "Plusonegame"

Problem Statement

Hero plays a game with a deck of cards and a counter. Initially, the counter is set to zero. During the game Hero must play each card in the deck exactly once. He gets to choose the order in which he plays the cards. You are given the description of the deck in the String s. Each character of s is either '+' or a digit ('0'-'9'). Each character represents one card, as described below.
  • Whenever Hero plays a card with a '+', the counter is incremented. (I.e., its value is increased by 1.)
  • Whenever Hero plays a card with a digit, he gets some (possibly zero) penalty points. The number of penalty points is calculated as abs(C - D), where C is the current value of the counter and D is the digit on the card.
Hero wants to minimize the sum of penalty points he receives during the game. Find an order in which he should play the cards, and return that order in a String. If there are multiple optimal orders of cards, return the lexicographically smallest among them.

Definition

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

Notes

  • Let S and T be two different but equally long strings, and let i be the smallest index such that S[i] and T[i] differ. We say that S is lexicographically smaller than T if the character S[i] has a smaller ASCII value than the character T[i].
  • The ASCII value of the '+' character is smaller than the ASCII values of digits.

Constraints

  • s will contain between 1 and 50 characters, inclusive.
  • Each character in s will be either '+' or digit('0' - '9').

Examples

  1. "1++"

    Returns: "+1+"

    Hero should increment the counter to 1, then play the card '1' for 0 penalty points, and then increment the counter again.

  2. "549"

    Returns: "459"

    Each of the six possible permutations of these cards leads to the same result: Hero will receive 4+5+9 = 18 penalty points. The string "459" is the lexicographically smallest of the six possible strings.

  3. "++++++"

    Returns: "++++++"

  4. "+++++2+"

    Returns: "++2++++"

  5. "++++4++++200++2++1+6++++++"

    Returns: "00+1+22++4++6+++++++++++++"

  6. "++11199999"

    Returns: "+111+99999"

  7. "++48++++7+5+++4++2+++7+829+3+++4+++7++++++"

    Returns: "++22+3+444+5++777+88+9++++++++++++++++++++"

  8. "608+28590++8340689++8+93+8+7+6+4077+067420"

    Returns: "000000++22+33+444+5+6666+7777+888888+999++"

  9. "0+7735++1263020485214+19449+07887+++++++1+"

    Returns: "0000+1111+222+33+4444+55+6+7777+888+99++++"

  10. "8+892537++08618+2880856++282594+819692+++115228729"

    Returns: "00+1111+22222222+3+4+5555+666+77+8888888888+99999+"

  11. "+++5++++++++++++7+++++"

    Returns: "+++++5++7+++++++++++++"

  12. "6++++++++++++++++++++++++++++2++++++++++++++"

    Returns: "++2++++6++++++++++++++++++++++++++++++++++++"

  13. "32851++4+++237833223++7++++13++5+2+4+0+05++650"

    Returns: "000+11+22222+333333+44+5555+6+77+88+++++++++++"

  14. "+++++++++++++++++++++++++++++++++++++++++++++"

    Returns: "+++++++++++++++++++++++++++++++++++++++++++++"

  15. "5+0+++++13+4319++8709+7+8+10+4+1++236++6796++++"

    Returns: "000+1111+2+333+44+5+666+777+88+999+++++++++++++"

  16. "+++++++5+++++++++++++++++++++++++++++++++9+4+++6++"

    Returns: "++++4+5+6+++9+++++++++++++++++++++++++++++++++++++"

  17. "+++++++++++++3+++++++++9++570+++++++++5+3+++7++++"

    Returns: "0+++33++55++77++9++++++++++++++++++++++++++++++++"

  18. "31534+98+5874++60949+4+0+9++784+5+82+955++"

    Returns: "00+1+2+33+44444+55555+6+77+8888+99999+++++"

  19. "27"

    Returns: "27"

  20. "30587399881469"

    Returns: "01334567888999"

  21. "+73689+3748+++1652362++9++++361+662505+6043+"

    Returns: "00+11+222+33333+44+555+6666666+77+88+99+++++"

  22. "+++++++++"

    Returns: "+++++++++"

  23. "+80133++92"

    Returns: "0+1+2+3389"

  24. "+188+31+5+9804+9578+4++59"

    Returns: "0+11++3+44+555++7+8888999"

  25. "+04+++++++37++3++3++8++4++++8++++96++++"

    Returns: "0+++333+44++6+7+88+9+++++++++++++++++++"

  26. "++++++++1++1+++++++++++++++++"

    Returns: "+11++++++++++++++++++++++++++"

  27. "+++++632"

    Returns: "++2+3++6"

  28. "1192+29368632258++00980577710590752466935580+7"

    Returns: "000000+111+22222+333+4555555666677777888899999"

  29. "9330686+15+4"

    Returns: "0+1+33456689"

  30. "0+1226694+19+565+64++31816++9+254202++"

    Returns: "00+1111+22222+3+444+555+66666++8+999++"

  31. "54+++2+++++++++++++9++++++7+++++2++8+8+++65+7+++"

    Returns: "++22++4+55+6+77+88+9++++++++++++++++++++++++++++"

  32. "13+++7++++++8+++++++++41+++4"

    Returns: "+11++3+44+++7+8+++++++++++++"

  33. "+70+1335708+0+8+32+81++"

    Returns: "000+11+2+333++5++77+888"

  34. "748379+56+0836++"

    Returns: "0+++33+456677889"

  35. "2+94+7+++79+9++34+0098+09+83147952+81306088940+837"

    Returns: "000000+11+22+3333+4444+5+6+7777+888888+9999999++++"

  36. "+++++36++++++0++68+4+++++9++++6+880++56+4+8++++++3"

    Returns: "00+++33+44+5+6666++8888+9+++++++++++++++++++++++++"

  37. "4+7++12+1++067+5475+88++++50++47+7+0++51+7++9268+"

    Returns: "000+111+22++444+5555+66+777777+888+9+++++++++++++"

  38. "++++"

    Returns: "++++"

  39. "6++1+7+++483+++3++60+93+1+4++70+2"

    Returns: "00+11+2+333+44++66+77+8+9++++++++"

  40. "++71+++0+++6+++++7+999++++8++8"

    Returns: "0+1+++++6+77+88+999+++++++++++"

  41. "++0++++++++++++++++++++++++++++++++++++++"

    Returns: "0++++++++++++++++++++++++++++++++++++++++"

  42. "+++7+++++++5+++++++0++++++++++++"

    Returns: "0+++++5++7++++++++++++++++++++++"

  43. "+++50+6++8++++++2262+080+30+0+"

    Returns: "00000++222+3++5+66++88++++++++"

  44. "155+++22+++87+0+9"

    Returns: "0+1+22+++55++7+89"

  45. "9512373083295463981135927353217918782810171111"

    Returns: "0011111111111222223333333455556777778888899999"

  46. "1253982+804+540307508594+425165929368871512074"

    Returns: "00000+1111+22222+33344444555555566777888889999"

  47. "+++4+90+3+3+99+3+++6+++91+++++++++67+++2+"

    Returns: "0+1+2+333+4++66+7++9999++++++++++++++++++"

  48. "29+35085593296+167525143591"

    Returns: "0+111+222333455555566789999"

  49. "2087333+18829+0+25+29877530543+4+4"

    Returns: "000+1+2222+33333+444+555+777888899"

  50. "4833118393+30+117+8++85320218++899938904+80+190703"

    Returns: "000000+111111+22+33333333+44+5++77+88888888+999999"

  51. "071006151193857748+15820543188512"

    Returns: "0000+1111111223344555556777888889"

  52. "6++3+23++18+9+7++59+3+++47+43++280"

    Returns: "0+1+22+3333+44+5+6+77+88+99+++++++"

  53. "+++46++++++++++"

    Returns: "++++4++6+++++++"

  54. "++++++1++++++0+++++++++++++"

    Returns: "0+1++++++++++++++++++++++++"

  55. "+++++++++++++++++++++6++++++++++5++9+++++++++9+"

    Returns: "+++++5+6+++99++++++++++++++++++++++++++++++++++"

  56. "382++5++269+++740+2+4+++4+++++7+++4++9++++++++1+++"

    Returns: "0+1+222+3+4444+5+6+77+8+99++++++++++++++++++++++++"

  57. "45099303++7+4+41426++74836+0++46+8+90412+++6+8342"

    Returns: "0000+11+222+3333+44444444+5+6666+77+888+999++++++"

  58. "0+7+1+++2++++4++1+8+3++3236+8+9+++1+0+75+2+"

    Returns: "00+111+222+333+4+5+6+77+88+9+++++++++++++++"

  59. "1+++++66+920++97+++++2+39++64+++6+++0+0"

    Returns: "000+1+22+3+4++6666+7++999++++++++++++++"

  60. "++++++4++4+++++9+9+2++++++8+++++++8+1+++1+90"

    Returns: "0+11+2++44++++88+999++++++++++++++++++++++++"

  61. "4173+1+67277+774151510508418+9"

    Returns: "00+111111+2+3+4445556777777889"

  62. "9030640461916590+2+979069738"

    Returns: "00000+11+2334456666778999999"

  63. "6072172+++08+4+9+24++5+++4+26+8++5+++77++++6"

    Returns: "00+1+2222++444+55+666+7777+88+9+++++++++++++"

  64. "53+++++03+8012146852277601945+68++15"

    Returns: "000+1111+222+33+44+5555+666+77+888+9"

  65. "++4+997++3"

    Returns: "+++3+4+799"

  66. "87+++52+6+355805+4516+2++07331+14+046+37+45475600"

    Returns: "00000+111+22+3333+44444+5555555+6666+7777+88+++++"

  67. "3+++++6+++1+5++++6+4++++5707+"

    Returns: "0+1++3+4+55+66+77++++++++++++"

  68. "++++++++33+01++++++3++++9+++8+4+++++++++++56+++"

    Returns: "0+1++333+4+5+6++8+9++++++++++++++++++++++++++++"

  69. "++7++9+1+++8+2+++++49++3+++++++7829++47+29"

    Returns: "+1+222+3+44+++777+88+9999+++++++++++++++++"

  70. "++312004++++++++++++++5+++++++++8++++6+++++6"

    Returns: "00+1+2+3+4+5+66++8++++++++++++++++++++++++++"

  71. "30359+54+75+89900902276511+3685646+8679952"

    Returns: "0000+11+222+333+44+55555566666777888999999"

  72. "1564915+709+83053+95++97452"

    Returns: "00+11+2+33+44+5555567789999"

  73. "1+++++7++++++"

    Returns: "+1++++++7++++"

  74. "1+++02+++++7+++++4++++44+++++0++2+++++"

    Returns: "00+1+22++444+++7++++++++++++++++++++++"

  75. "7054558826359499967329046666945+586850076++40460"

    Returns: "000000++22+3344444455555556666666667778888999999"

  76. "6684126076266092167840+678287759261967+2+269730899"

    Returns: "0000+111+2222222+344566666666666777777788888999999"

  77. "+++1++"

    Returns: "+1++++"

  78. "+9"

    Returns: "+9"

  79. "++2364583++847++100+"

    Returns: "00+1+2+33+44+5+6+788"

  80. "+++++++++++++++"

    Returns: "+++++++++++++++"

  81. "2347899876667809111+++++++1000000000"

    Returns: "0000000000+1111+2+3+4++666+777888999"

  82. "987654321"

    Returns: "123456789"

  83. "+++++++23"

    Returns: "++2+3++++"

  84. "++++++++++++++++++++++++"

    Returns: "++++++++++++++++++++++++"

  85. "33322111++"

    Returns: "+111+22333"

  86. "+++00"

    Returns: "00+++"


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: