Problem Statement
Given a non-negative integer n (n may have up to 50 digits) and another number m, remove exactly m digits from n so the remaining number is as big as possible. m will always be less than the number of digits in n.
Create a class RemoveDigits containing the method maxNumber which takes a
Definition
- Class:
- RemoveDigits
- Method:
- maxNumber
- Parameters:
- String, int
- Returns:
- String
- Method signature:
- String maxNumber(String n, int m)
- (be sure your method is public)
Notes
- n may contain leading zeros, see example 1.
- The return value should contain exactly |n|-m digits, where |n| is the number of digits in n.
Constraints
- n will contain between 1 and 50 characters, inclusive.
- n will only contain digits ('0'-'9').
- m will be between 0 and |n|-1, inclusive, where |n| is the number of digits in n.
Examples
"834391370"
2
Returns: "8491370"
We should try to get the highest digits in the front. It's not possible to get the 9 in the front since that would require removing 4 digits, so we have to be satisfied with the 8. We still can't get the 9 to the second place in the final number, so the 4 is the highest choice for this position and thus we remove the digit 3. Now we can get the 9 as the third digit from the left in the final number by removing the second digit 3. Now we have removed 2 digits, and the number remaining is 8491370.
"0021597002943020"
3
Returns: "2597002943020"
We start with removing the two leading zeros and then the first 1, thus getting 2597002943020.
"0000500390"
2
Returns: "00500390"
"91459800928398113866079548895152924499"
22
Returns: "9999995152924499"
"98"
1
Returns: "9"
"5574742579580037796"
5
Returns: "77579580037796"
"9"
0
Returns: "9"
"97196906765509955148247685136485989590567213"
24
Returns: "99999888989590567213"
"045976155811530869432464"
21
Returns: "996"
"5042704197833986905959"
3
Returns: "5704197833986905959"
"46015497067739966839147010850932314827724"
6
Returns: "97067739966839147010850932314827724"
"837050534000993078357423071802503680066009"
32
Returns: "9988866009"
"5913923747221068072017099345521753667"
26
Returns: "99995753667"
"681"
2
Returns: "8"
"421419323596316821426638394694"
6
Returns: "933596316821426638394694"
"53629987"
0
Returns: "53629987"
"97153"
4
Returns: "9"
"07528682747096067186314583904499"
4
Returns: "8682747096067186314583904499"
"51105949004125"
2
Returns: "515949004125"
"4532805243631322601073622502317899126"
28
Returns: "877899126"
"79419286675357073617180186979768641433121839086"
29
Returns: "999986443321839086"
"27101821"
0
Returns: "27101821"
"735674297930366516473023942133059"
1
Returns: "75674297930366516473023942133059"
"858305306098129664314486"
22
Returns: "99"
"203516274775843435431"
1
Returns: "23516274775843435431"
"35173303638285773727812555"
4
Returns: "7333638285773727812555"
"3519988270381272986302275"
9
Returns: "9988872986302275"
"0053713395864221963086642659889255782345353"
37
Returns: "999985"
"931327225382557685702445340295"
0
Returns: "931327225382557685702445340295"
"91228486230828791783122"
3
Returns: "98486230828791783122"
"5"
0
Returns: "5"
"2260280513100029014888065551395552151441018"
0
Returns: "2260280513100029014888065551395552151441018"
"8367212524761746221169322432511"
13
Returns: "877762269322432511"
"9404726293910"
4
Returns: "976293910"
"5906208116615"
8
Returns: "98665"
"2760764491237019"
7
Returns: "791237019"
"55878243502579215533780350587"
2
Returns: "878243502579215533780350587"
"9218429133958991341631632306872134628"
33
Returns: "9999"
"68967749393828728066"
0
Returns: "68967749393828728066"
"525479"
2
Returns: "5579"
"476479057050978798223962065952324"
25
Returns: "99999534"
"86595156412"
4
Returns: "9556412"
"460776853273467149443033352630411929436"
17
Returns: "9443033352630411929436"
"982349234294189273489273418273489237438"
38
Returns: "9"
"5390414380340543465274678742905860863658759366621"
47
Returns: "99"
"0"
0
Returns: "0"
"00"
1
Returns: "0"
"789"
2
Returns: "9"
"99999999999999999999999999999999999999999999999999"
1
Returns: "9999999999999999999999999999999999999999999999999"
"987"
1
Returns: "98"
"1337"
0
Returns: "1337"
"123"
0
Returns: "123"
"9999"
1
Returns: "999"
"9999999999999999999999999999999999999999"
5
Returns: "99999999999999999999999999999999999"
"001100"
5
Returns: "1"
"999999"
3
Returns: "999"
"1928374656473625278234"
13
Returns: "987778234"