I would suggest this approach:
public class Solution {
public static void main(final String args[]) {
final int upToNumberNonInclusive = 2;
final int upToLength = 2;
appendPermutations(upToNumberNonInclusive, upToLength, new Appendable() {
@Override
public void append(final CharSequence text) {
System.out.println(text);
}
});
}
/**
* Generates all Permutations from 0..upToNumberNonInclusive with a length of upToLength.<br>
* Example: upToNumberNonInclusive=2, upToLength=2<br>
* result: 0, 1, 10, 11 (arguments for append)
*/
public static void appendPermutations(final int upToNumberNonInclusive, final int upToLength, final Appendable appendable) {
for (int number = 0; number < upToNumberNonInclusive; number++)
appendPermutationsHelper(upToNumberNonInclusive, number, upToLength - 1, appendable);
}
private static void appendPermutationsHelper(final int numberOfForks, final int currentNumber, final int depth, final Appendable appendable) {
if (depth == 0) {
appendable.append(Integer.toString(currentNumber, 10));
return;
}
for (int number = 0; number < numberOfForks; number++)
appendPermutationsHelper(numberOfForks, currentNumber * 10 + number, depth - 1, appendable);
}
public interface Appendable {
public void append(CharSequence text);
}
}
The recursion makes more clarity and avoiding StringBuilder calls would create less object actions.
If you need the exact same solution as in the example code, you can add a method to fill it up:
public class Solution {
public static void main(final String args[]) {
final int upToNumberNonInclusive = 2;
final int upToLength = 2;
appendPermutations(upToNumberNonInclusive, upToLength, new Appendable() {
private final static char fillCharacter = '0';
private static final String endOfLine = "\"\r\n\"";
@Override
public void append(final CharSequence text) {
System.out.print(fillUpText(text) + endOfLine);
}
/** fills up the text up to upToLength with fillCharacter */
private CharSequence fillUpText(final CharSequence text) {
if (text.length() >= upToLength)
return text;
final char[] arrayChar = new char[upToLength - text.length()];
Arrays.fill(arrayChar, fillCharacter);
return new String(arrayChar) + text;
}
});
}
/**
* Generates all Permutations from 0..upToNumberNonInclusive with a length of upToLength.<br>
* Example: upToNumberNonInclusive=2, upToLength=2<br>
* result: 0, 1, 10, 11 (arguments for append)
*/
public static void appendPermutations(final int upToNumberNonInclusive, final int upToLength, final Appendable appendable) {
for (int number = 0; number < upToNumberNonInclusive; number++)
appendPermutationsHelper(upToNumberNonInclusive, number, upToLength - 1, appendable);
}
private static void appendPermutationsHelper(final int numberOfForks, final int currentNumber, final int depth, final Appendable appendable) {
if (depth == 0) {
appendable.append(Integer.toString(currentNumber, 10));
return;
}
for (int number = 0; number < numberOfForks; number++)
appendPermutationsHelper(numberOfForks, currentNumber * 10 + number, depth - 1, appendable);
}
public interface Appendable {
public void append(CharSequence text);
}
}
Please notice, that the anonymous class is only for educational purpose, not the suggested general solution.
.append()actually does, that is your only inefficiency I see! – Jarrod Roberson Jan 16 at 2:02