Statistics

Problem Statement for "LeftRightDigitsGame"

Problem Statement

You are playing a solitaire game called Left-Right Digits Game. This game uses a deck of N cards. Each card has a single digit written on it. These digits are given as characters in the String digits. More precisely, the i-th character of digits is the digit written on the i-th card from the top of the deck (both indices are 0-based).

The game is played as follows. First, you place the topmost card (whose digit is the 0-th character of digits) on the table. Then, you pick the cards one-by-one from the top of the deck. For each card, you have to place it either to the left or to the right of all cards that are already on the table.

After all of the cards have been placed on the table, they now form an N-digit number. This number must not have leading zeros; i.e., the leftmost card on the table must not be a '0'. The goal of the game is to make this N-digit number as small as possible.

Return the smallest possible resulting number you can achieve in this game as a String.

Definition

Class:
LeftRightDigitsGame
Method:
minNumber
Parameters:
String
Returns:
String
Method signature:
String minNumber(String digits)
(be sure your method is public)

Constraints

  • digits will contain between 1 and 50 characters, inclusive.
  • Each character of digits will be between '0' and '9', inclusive.
  • digits will contain at least one character that is not '0'.

Examples

  1. "565"

    Returns: "556"

    The solution is as follows. Place the first card on the table. Place the second card to the right of all cards on the table. Place the last card to the left of all cards on the table.

  2. "9876543210"

    Returns: "1234567890"

    The resulting number cannot have leading zeros.

  3. "8016352"

    Returns: "1086352"

  4. "1"

    Returns: "1"

  5. "01"

    Returns: "10"

  6. "901"

    Returns: "109"

  7. "109"

    Returns: "109"

  8. "380"

    Returns: "380"

  9. "3928360770"

    Returns: "2398360770"

  10. "284571068549373"

    Returns: "128457068549373"

  11. "69727879252051753520"

    Returns: "10222697787955753520"

  12. "53013652946745933727468097755950461"

    Returns: "10003513652946745933727468977559546"

  13. "7482528283429368756946791671134092246285"

    Returns: "1112222478588349368756946796734092246285"

  14. "849977772669238327630051487226702890699855771"

    Returns: "100002224899777766938376351487226728969985577"

  15. "82358880314196476696612703282192340320351990759311"

    Returns: "10000028358883141964766966127328219234323519975931"

  16. "09246590521466069264983001526938055924978335933152"

    Returns: "10000009246595214666926498315269385592497833593352"

  17. "57017243424714583743738007458176899623185588138523"

    Returns: "10005717243424714583743738745817689962318558838523"

  18. "01682858369148036469534345822052230638496300323191"

    Returns: "10000001682858369148364695343458225223638496332319"

  19. "0446"

    Returns: "4046"

  20. "73890136300"

    Returns: "10378936300"

  21. "9921454865020000"

    Returns: "1299454865020000"

  22. "917043802938059265000"

    Returns: "197043802938059265000"

  23. "69250700000000000000000000"

    Returns: "26950700000000000000000000"

  24. "7983118308482670950633434269600"

    Returns: "1137988308482670950633434269600"

  25. "792086082908829300000000000000000000"

    Returns: "200027986829889300000000000000000000"

  26. "6839813778566800435979730544509492376873629420000"

    Returns: "1368983778566800435979730544509492376873629420000"

  27. "42793539048451947810651184791197366812826683980000"

    Returns: "10024793539484519478165118479119736682826683980000"

  28. "61768559846114246084877077858914291066143954400000"

    Returns: "10001116768559846424684877778589142916643954400000"

  29. "66033001916429687820706397816532137759000000000000"

    Returns: "10000066331916429687827639781653237759000000000000"

  30. "84291027751908867839544430938728149252129230000000"

    Returns: "10001248927751988678395444393872814925229230000000"

  31. "05307"

    Returns: "30507"

  32. "007534679"

    Returns: "300754679"

  33. "00090228349336"

    Returns: "20000928349336"

  34. "0000000000868862883"

    Returns: "2000000000086886883"

  35. "005298559466821452550331"

    Returns: "100052985594668214525533"

  36. "00009804003774430273936408859"

    Returns: "20000000098437744373936408859"

  37. "00000000000000000000000000000000000007301376"

    Returns: "10000000000000000000000000000000000000073376"

  38. "000008119208188278916481616375140448269301319248"

    Returns: "100000000811928188278916481616375144482693139248"

  39. "009090"

    Returns: "900090"

  40. "005461207440"

    Returns: "100546207440"

  41. "0031351452000000"

    Returns: "1003135452000000"

  42. "0000000033528753020000"

    Returns: "2000000000335287530000"

  43. "00005816178913475263885000"

    Returns: "10000581617893475263885000"

  44. "00000000004000000000000000700000"

    Returns: "40000000000000000000000000700000"

  45. "0000000008877678200000000000000000000000000000"

    Returns: "2000000000887767800000000000000000000000000000"

  46. "00000161981928999392773240000000000000000000000"

    Returns: "10000016198928999392773240000000000000000000000"

  47. "00000000000000000000000000000000000019543005591400"

    Returns: "10000000000000000000000000000000000000019543559400"

  48. "00021700814855832354004145445069391684775100000000"

    Returns: "10000000021781485583235441454456939168477500000000"

  49. "00000031262631020759000000000000000000000000000000"

    Returns: "10000003126263020759000000000000000000000000000000"

  50. "00000016518063134132359866000000000000000000000000"

    Returns: "10000000165186313432359866000000000000000000000000"

  51. "0000040000"

    Returns: "4000000000"

  52. "00007050000000000000"

    Returns: "50000070000000000000"

  53. "000000000000100000000001000070"

    Returns: "100000000000000000000001000070"

  54. "000000000000000000000000907000"

    Returns: "700000000000000000000000009000"

  55. "0000000010000600000007000000000000000000"

    Returns: "1000000000000600000007000000000000000000"

  56. "0000000080003010000000000000009000700000"

    Returns: "1000000000000830000000000000009000700000"

  57. "02800000007000060000006000000000000000000000000000"

    Returns: "20800000007000060000006000000000000000000000000000"

  58. "00007000000002000000000000300000000000030000000800"

    Returns: "20000000000007000000000000300000000000030000000800"

  59. "00000000000000000000000000000000000000000007000000"

    Returns: "70000000000000000000000000000000000000000000000000"

  60. "00000000004000000000040206009800070004000000000807"

    Returns: "20000000000000000000004406009800070004000000000807"

  61. "04000080781005700670000060008000000000300040009500"

    Returns: "10000004878005700670000060008000000000300040009500"

  62. "00000090220307000005200060950071860480030100020001"

    Returns: "10000000000000000000000000000009223752695718648312"

  63. "09900658900028087062800327052300300063500509267033"

    Returns: "20000000000000000000996589288762832752336355967033"

  64. "80201111118020111111802011111180201111118020111900"

    Returns: "10000000000821111118211111182111111821111118211900"

  65. "100218000475729999884"

    Returns: "100128000475729999884"

  66. "7701020304050607080900010203040506070809001234589"

    Returns: "1000000000000000000000077123456789123456789234589"

  67. "00056168912543213261112346598646132900065198440000"

    Returns: "10000005616891254321326111234659864613296598440000"

  68. "18412038135123"

    Returns: "10118423813523"

  69. "504030306090805020105030605040450260990"

    Returns: "100000000054336985205030605040450260990"

  70. "0000000000000012345678909876543211234567890000000"

    Returns: "1000000000000000123456789987654321234567890000000"

  71. "124012051250012540001200"

    Returns: "100000001241251251254200"

  72. "00090501000"

    Returns: "10000095000"

  73. "10234567891023456789102345678910234567891023456789"

    Returns: "10000123456789123456789123456789123456789023456789"

  74. "1840001230005550006660007070888802030608090102011"

    Returns: "1000000000000000000000018412355566677888823689121"

  75. "2155124120152012501205000"

    Returns: "1000111255242152125205000"

  76. "12345678901234567890123456789012345678901234567890"

    Returns: "10000123456789123456789123456789123456789234567890"

  77. "123423255412342325541234232554123042325540000"

    Returns: "111123423255423423255423423255423042325540000"

  78. "00021"

    Returns: "10002"

  79. "000005000020001"

    Returns: "100000000000052"

  80. "02011"

    Returns: "10021"

  81. "900000000000000000000000000000000080000000000000"

    Returns: "800000000000000000000000000000000090000000000000"

  82. "1010101"

    Returns: "1000111"

  83. "78965436914785196321956498759265398418523100187546"

    Returns: "10011111345678969478596329564987592653984852387546"

  84. "010201"

    Returns: "100012"

  85. "8016352000"

    Returns: "1086352000"

  86. "7812211207816731125133548112591212139"

    Returns: "1011178222781673112513354811259121239"

  87. "99999999999999999999999999999999999999999909999999"

    Returns: "90999999999999999999999999999999999999999999999999"

  88. "10003"

    Returns: "10003"

  89. "1234567890000123456789123456789123456789123456789"

    Returns: "1000012345678912345678912345678912345678923456789"

  90. "5009001200010000029870010023311000"

    Returns: "1000000000000000059121298712331000"

  91. "081016352"

    Returns: "100816352"

  92. "010203040506070809090807060504030201010203040506"

    Returns: "100000000000000000001234567899876543210203040506"

  93. "10101098"

    Returns: "10011098"

  94. "80101010101052"

    Returns: "10000081111052"

  95. "1009"

    Returns: "1009"

  96. "3874533543545353445645663286786780000004553544"

    Returns: "2333333387455454554456456686786780000004553544"

  97. "102030111"

    Returns: "100012311"

  98. "01234067890123456789002345678901234067890123406089"

    Returns: "10000000012346789123456789234567891234678923406089"

  99. "801010101010101010101010101010101010101010101052"

    Returns: "100000000000000000000008111111111111111111111052"

  100. "030101"

    Returns: "100031"

  101. "3675356291270936062618792023759228973612931947845"

    Returns: "1000123367556927936626187922375922897361293947845"

  102. "987654321005200154661234567895464312121544545459"

    Returns: "100001234567895215466123456789546431212544545459"

  103. "00206090951905040454010502228822022088022910283"

    Returns: "10000000000000026995195445415222882222882290283"

  104. "01921021201"

    Returns: "10001921212"

  105. "20430"

    Returns: "20430"

  106. "000500048787687787876787853401234510867687045600"

    Returns: "100000005487876877878767878534123450867687045600"

  107. "00321"

    Returns: "10032"


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: