Split text into sub-strings according to below rules:
- a) The length of each sub-string should less than or equal to M
- b) The length of sub-string should less than or equal to N (N < M) if the sub-string contains any numeric char
- c) The total number of sub-strings should be as small as possible
I have no clue how to solve this question, I guess it is related to “dynamic programming”.
Can anybody help me implement it using C# or Java? Thanks a lot.
Idea
A greedy approach is the way to go:
Proof
Here’s a reductio-ad-absurdum proof that the above yields an optimal solution.
Assume there is a better split than the greedy split. Let’s skip to the point where the two splits start to differ and remove everything before this point.
Case 1) A digit among the first N characters.
Assume that there is an input for which chopping off the first N characters cannot yield an optimal solution.
However, the second segment of the putative better solution can be always shortened from the left side, and the first one extended to N characters, without altering the number of segments. Therefore, a contradiction: this split is not better than the greedy split.
Case 2) No digit among the first K (N < K <= M) characters.
Assume that there is an input for which chopping off the first K characters cannot yield an optimal solution.
Again, the the “better” split can be transformed, without altering the number of segments, to the greedy split, which contradicts the initial assumption that there is a better split than the greedy split.
Therefore, the greedy split is optimal. Q.E.D.
Implementation (Python)
Implementation (Java)