Problem Statement
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
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
"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.
"9876543210"
Returns: "1234567890"
The resulting number cannot have leading zeros.
"8016352"
Returns: "1086352"
"1"
Returns: "1"
"01"
Returns: "10"
"901"
Returns: "109"
"109"
Returns: "109"
"380"
Returns: "380"
"3928360770"
Returns: "2398360770"
"284571068549373"
Returns: "128457068549373"
"69727879252051753520"
Returns: "10222697787955753520"
"53013652946745933727468097755950461"
Returns: "10003513652946745933727468977559546"
"7482528283429368756946791671134092246285"
Returns: "1112222478588349368756946796734092246285"
"849977772669238327630051487226702890699855771"
Returns: "100002224899777766938376351487226728969985577"
"82358880314196476696612703282192340320351990759311"
Returns: "10000028358883141964766966127328219234323519975931"
"09246590521466069264983001526938055924978335933152"
Returns: "10000009246595214666926498315269385592497833593352"
"57017243424714583743738007458176899623185588138523"
Returns: "10005717243424714583743738745817689962318558838523"
"01682858369148036469534345822052230638496300323191"
Returns: "10000001682858369148364695343458225223638496332319"
"0446"
Returns: "4046"
"73890136300"
Returns: "10378936300"
"9921454865020000"
Returns: "1299454865020000"
"917043802938059265000"
Returns: "197043802938059265000"
"69250700000000000000000000"
Returns: "26950700000000000000000000"
"7983118308482670950633434269600"
Returns: "1137988308482670950633434269600"
"792086082908829300000000000000000000"
Returns: "200027986829889300000000000000000000"
"6839813778566800435979730544509492376873629420000"
Returns: "1368983778566800435979730544509492376873629420000"
"42793539048451947810651184791197366812826683980000"
Returns: "10024793539484519478165118479119736682826683980000"
"61768559846114246084877077858914291066143954400000"
Returns: "10001116768559846424684877778589142916643954400000"
"66033001916429687820706397816532137759000000000000"
Returns: "10000066331916429687827639781653237759000000000000"
"84291027751908867839544430938728149252129230000000"
Returns: "10001248927751988678395444393872814925229230000000"
"05307"
Returns: "30507"
"007534679"
Returns: "300754679"
"00090228349336"
Returns: "20000928349336"
"0000000000868862883"
Returns: "2000000000086886883"
"005298559466821452550331"
Returns: "100052985594668214525533"
"00009804003774430273936408859"
Returns: "20000000098437744373936408859"
"00000000000000000000000000000000000007301376"
Returns: "10000000000000000000000000000000000000073376"
"000008119208188278916481616375140448269301319248"
Returns: "100000000811928188278916481616375144482693139248"
"009090"
Returns: "900090"
"005461207440"
Returns: "100546207440"
"0031351452000000"
Returns: "1003135452000000"
"0000000033528753020000"
Returns: "2000000000335287530000"
"00005816178913475263885000"
Returns: "10000581617893475263885000"
"00000000004000000000000000700000"
Returns: "40000000000000000000000000700000"
"0000000008877678200000000000000000000000000000"
Returns: "2000000000887767800000000000000000000000000000"
"00000161981928999392773240000000000000000000000"
Returns: "10000016198928999392773240000000000000000000000"
"00000000000000000000000000000000000019543005591400"
Returns: "10000000000000000000000000000000000000019543559400"
"00021700814855832354004145445069391684775100000000"
Returns: "10000000021781485583235441454456939168477500000000"
"00000031262631020759000000000000000000000000000000"
Returns: "10000003126263020759000000000000000000000000000000"
"00000016518063134132359866000000000000000000000000"
Returns: "10000000165186313432359866000000000000000000000000"
"0000040000"
Returns: "4000000000"
"00007050000000000000"
Returns: "50000070000000000000"
"000000000000100000000001000070"
Returns: "100000000000000000000001000070"
"000000000000000000000000907000"
Returns: "700000000000000000000000009000"
"0000000010000600000007000000000000000000"
Returns: "1000000000000600000007000000000000000000"
"0000000080003010000000000000009000700000"
Returns: "1000000000000830000000000000009000700000"
"02800000007000060000006000000000000000000000000000"
Returns: "20800000007000060000006000000000000000000000000000"
"00007000000002000000000000300000000000030000000800"
Returns: "20000000000007000000000000300000000000030000000800"
"00000000000000000000000000000000000000000007000000"
Returns: "70000000000000000000000000000000000000000000000000"
"00000000004000000000040206009800070004000000000807"
Returns: "20000000000000000000004406009800070004000000000807"
"04000080781005700670000060008000000000300040009500"
Returns: "10000004878005700670000060008000000000300040009500"
"00000090220307000005200060950071860480030100020001"
Returns: "10000000000000000000000000000009223752695718648312"
"09900658900028087062800327052300300063500509267033"
Returns: "20000000000000000000996589288762832752336355967033"
"80201111118020111111802011111180201111118020111900"
Returns: "10000000000821111118211111182111111821111118211900"
"100218000475729999884"
Returns: "100128000475729999884"
"7701020304050607080900010203040506070809001234589"
Returns: "1000000000000000000000077123456789123456789234589"
"00056168912543213261112346598646132900065198440000"
Returns: "10000005616891254321326111234659864613296598440000"
"18412038135123"
Returns: "10118423813523"
"504030306090805020105030605040450260990"
Returns: "100000000054336985205030605040450260990"
"0000000000000012345678909876543211234567890000000"
Returns: "1000000000000000123456789987654321234567890000000"
"124012051250012540001200"
Returns: "100000001241251251254200"
"00090501000"
Returns: "10000095000"
"10234567891023456789102345678910234567891023456789"
Returns: "10000123456789123456789123456789123456789023456789"
"1840001230005550006660007070888802030608090102011"
Returns: "1000000000000000000000018412355566677888823689121"
"2155124120152012501205000"
Returns: "1000111255242152125205000"
"12345678901234567890123456789012345678901234567890"
Returns: "10000123456789123456789123456789123456789234567890"
"123423255412342325541234232554123042325540000"
Returns: "111123423255423423255423423255423042325540000"
"00021"
Returns: "10002"
"000005000020001"
Returns: "100000000000052"
"02011"
Returns: "10021"
"900000000000000000000000000000000080000000000000"
Returns: "800000000000000000000000000000000090000000000000"
"1010101"
Returns: "1000111"
"78965436914785196321956498759265398418523100187546"
Returns: "10011111345678969478596329564987592653984852387546"
"010201"
Returns: "100012"
"8016352000"
Returns: "1086352000"
"7812211207816731125133548112591212139"
Returns: "1011178222781673112513354811259121239"
"99999999999999999999999999999999999999999909999999"
Returns: "90999999999999999999999999999999999999999999999999"
"10003"
Returns: "10003"
"1234567890000123456789123456789123456789123456789"
Returns: "1000012345678912345678912345678912345678923456789"
"5009001200010000029870010023311000"
Returns: "1000000000000000059121298712331000"
"081016352"
Returns: "100816352"
"010203040506070809090807060504030201010203040506"
Returns: "100000000000000000001234567899876543210203040506"
"10101098"
Returns: "10011098"
"80101010101052"
Returns: "10000081111052"
"1009"
Returns: "1009"
"3874533543545353445645663286786780000004553544"
Returns: "2333333387455454554456456686786780000004553544"
"102030111"
Returns: "100012311"
"01234067890123456789002345678901234067890123406089"
Returns: "10000000012346789123456789234567891234678923406089"
"801010101010101010101010101010101010101010101052"
Returns: "100000000000000000000008111111111111111111111052"
"030101"
Returns: "100031"
"3675356291270936062618792023759228973612931947845"
Returns: "1000123367556927936626187922375922897361293947845"
"987654321005200154661234567895464312121544545459"
Returns: "100001234567895215466123456789546431212544545459"
"00206090951905040454010502228822022088022910283"
Returns: "10000000000000026995195445415222882222882290283"
"01921021201"
Returns: "10001921212"
"20430"
Returns: "20430"
"000500048787687787876787853401234510867687045600"
Returns: "100000005487876877878767878534123450867687045600"
"00321"
Returns: "10032"