For what I see, there are a couple of things that should be improved.
---
As @Heslacher mentioned in his answer, naming should be changed into something better. I'd suggest to change:
public static int minSums(String numbers, int sum) {
int N = numbers.length();
into something like:
public static int minSums(String digits, int sum) {
int numberOfDigits = digits.length();
---
The current solution does a bit too much IMO. It parses the string and also finds the solutions. I'd split that in two parts: one which gets all the possible numbers starting from the original string and one which finds the solution based on the numbers taken from the additional method.
---
As an alternative solution, I'd suggest the following (in pseudo-code):
// this method should return all possible numbers in a string
// of digits. For "1234" it should return {1,2,3,4,12,23,34,123,234}
int[] getPossibleNumbers(String digits){
char[] chars = digits.toCharArray();
List<int> numbers = new List<int>();
int helper;
int zero = 48; // this is the char val for the '0' digit
for(int i = 0; i < chars.length; i++){
for(int j = 1; j < chars.length; j++){
helper = 0;
for(int k = 0; k < j && (i + k) < chars.length; k++){
helper = (helper * 10) + ((int)chars[i + k]) - zero;
}
numbers.append(helper);
}
}
return numbers.toArray();
}
int minSums(String digits, int sum){
int[] numbers = getPossibleNumbers(digits);
Queue<SumStatus> backupQueue = new Queue<SumStatus>();
int numbersStartingIndex; // this index will be used to get the various numbers to sum
for(int i = 0; i < numbers.length; i++){
SumStatus status = new SumStatus(new List<int>().append(numbers[i]), numbers[i]);
backupQueue.push(status);
}
while(!backupQueue.isEmpty()){
SumStatus status = backupQueue.pop();
if(status.sum == sum){
// we found the min-sum
return status.numbers.length() - 1;
}
if(status.sum < sum){
// we have not found the min-sum yet
numbersStartingIndex = status.numbers.length();
for(int i = numbersStartingIndex; i < numbers.length; i++){
SumStatus newStatus = new SumStatus(status.numbers, status.sum);
newStatus.numbers.append(numbers[i]);
newStatus.sum += numbers[i];
if(newStatus.sum <= sum){
backupQueue.push(status);
}
}
}
// when status.sum > sum the item is simply ignored and popped from the queue
}
// no possible combination found
return -1;
}
class SumStatus{
List<int> numbers;
int sum;
SumStatus(List<int> nums, int currentSum){
numbers = nums;
sum = currentSum;
}
}
The proposed solution does a [Breadth-First Search][1] in the [State Space][2] of the problem. The State Space is made of the numbers evaluated till that moment. The Breadth-First Search may not be immediatly visible because there's no tree to visit in a recursive fashion, but it's implemented via a `Queue<SumStatus>`.
Let me know if anything is unclear.
P.S.: The code is not functional code. It just serves as a means to clarify the idea I had.
[1]: https://en.wikipedia.org/wiki/Breadth-first_search
[2]: https://en.wikipedia.org/wiki/State_space