Advertisement
Guest User

UnlockPatternCombination

a guest
Jan 27th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1.     public static List<String> findCombo(){
  2.         char[][] keyboard = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
  3.         List<String> result = new ArrayList<String>();
  4.         String tmp = new String();
  5.         for(int i = 0; i < 3; i++) {
  6.             for (int j = 0; j < 3; j++) {
  7.                 helper(keyboard, result, tmp, i, j);
  8.             }
  9.         }
  10.         return result;
  11.     }
  12.  
  13.     public static void helper(char[][] keyboard, List<String> result, String tmp, int i, int j){
  14.         if(tmp.length()==4 && !result.contains(tmp)){
  15.             result.add(new String(tmp));
  16.             return;
  17.         }
  18.         if(i>=3||j>=3||i<0||j<0) return;
  19.  
  20.         char ch = keyboard[i][j];
  21.         if(tmp.indexOf(ch)<0){
  22.         //if(tmp.length()==0||(tmp.length()>0&&tmp.charAt(tmp.length()-1)!=ch)){
  23.             helper(keyboard, result, tmp+ch, i+1, j);
  24.             helper(keyboard, result, tmp+ch, i, j+1);
  25.             helper(keyboard, result, tmp+ch, i+1, j+1);
  26.             helper(keyboard, result, tmp+ch, i-1, j);
  27.             helper(keyboard, result, tmp+ch, i, j-1);
  28.             helper(keyboard, result, tmp+ch, i-1, j-1);
  29.             helper(keyboard, result, tmp+ch, i+1, j-1);
  30.             helper(keyboard, result, tmp+ch, i-1, j+1);
  31.         }
  32.     }
  33.  
  34.     public static void main(String args[]){
  35.         List<String> result = findCombo();
  36.         for(String str : result){
  37.             System.out.println(str);
  38.         }
  39.         System.out.println("Total combinations: "+result.size());
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement