Advertisement
todorvt

Combinations

Aug 27th, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. package combinations;
  2.  
  3. public class Combinations {
  4.  
  5.     public static void main(String[] args) {
  6.         // Create an alphabet to work with
  7.         char[] alphabet = new char[]{'1', '2', '3'};
  8.         // Find all possible combinations of this alphabet in the string size of 2
  9.         possibleStrings(2, alphabet, "", 0);
  10.     }
  11.  
  12.     public static void possibleStrings(int maxLength, char[] alphabet, String curr, int ind) {
  13.         // If the current string has reached it's maximum length
  14.         if (curr.length() == maxLength) {
  15.             System.out.println(curr);
  16.  
  17.             // Else add each letter from the alphabet to new strings and process these new strings again
  18.         } else {
  19.             for (int i = ind; i < alphabet.length; i++) {
  20.                 String oldCurr = curr;
  21.                 curr += alphabet[i];
  22.                 possibleStrings(maxLength, alphabet, curr, i);
  23.                 curr = oldCurr;
  24.             }
  25.         }
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement