You are given a string S made of N digits that represents a positive integer.
Among all positive integers smaller than S, find the one with the maximum possible sum of digits. If there is more than one such integer, return any of them. The returned string can only consist of digits and may not contain leading zeros.
Examples:
I have tried but coudn't get better solution than brute force. Is there any optimal way to do this?