Try it online here. Notice, this has a time (and space) complexity of O(n). Therefore the test case
12345678901234567890 takes too long in the online compiler. Use the offline one instead (1 minute on my laptop).
This is only my first attempt. There might be some room for improvements.
Detailed explanation will follow soon. Just some ideas how my algorithm works.
- I interpret the input as string and not as a number.
- Then I create all possible subsets of
[0, 1, 2, ..., len(n-1)]
- For each of these subsets (lets take
[1, 4, 5]), I split the input string into part using these numbers.
[input[0:1], input[1, 4], input[4,5], input[5,len(input)]].
- Afterwards I try to convert these numbers into strings. There can be two problems. Pyth (or Python) throws an exception for an empty string, and for a string of numbers starting with
0. Therefore I use a
try - catchblock (actually infinity loop with an immediate break). If converting was successfully, I add the result to a list
- After I handled all subsets, I filter the list
Yfor results, that are already sorted and print the last one (the last one has the most groups).