Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package combinations;
- public class Combinations {
- public static void main(String[] args) {
- // Create an alphabet to work with
- char[] alphabet = new char[]{'1', '2', '3'};
- // Find all possible combinations of this alphabet in the string size of 2
- possibleStrings(2, alphabet, "", 0);
- }
- public static void possibleStrings(int maxLength, char[] alphabet, String curr, int ind) {
- // If the current string has reached it's maximum length
- if (curr.length() == maxLength) {
- System.out.println(curr);
- // Else add each letter from the alphabet to new strings and process these new strings again
- } else {
- for (int i = ind; i < alphabet.length; i++) {
- String oldCurr = curr;
- curr += alphabet[i];
- possibleStrings(maxLength, alphabet, curr, i);
- curr = oldCurr;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement