Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static List<String> findCombo(){
- char[][] keyboard = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
- List<String> result = new ArrayList<String>();
- String tmp = new String();
- for(int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- helper(keyboard, result, tmp, i, j);
- }
- }
- return result;
- }
- public static void helper(char[][] keyboard, List<String> result, String tmp, int i, int j){
- if(tmp.length()==4 && !result.contains(tmp)){
- result.add(new String(tmp));
- return;
- }
- if(i>=3||j>=3||i<0||j<0) return;
- char ch = keyboard[i][j];
- if(tmp.indexOf(ch)<0){
- //if(tmp.length()==0||(tmp.length()>0&&tmp.charAt(tmp.length()-1)!=ch)){
- helper(keyboard, result, tmp+ch, i+1, j);
- helper(keyboard, result, tmp+ch, i, j+1);
- helper(keyboard, result, tmp+ch, i+1, j+1);
- helper(keyboard, result, tmp+ch, i-1, j);
- helper(keyboard, result, tmp+ch, i, j-1);
- helper(keyboard, result, tmp+ch, i-1, j-1);
- helper(keyboard, result, tmp+ch, i+1, j-1);
- helper(keyboard, result, tmp+ch, i-1, j+1);
- }
- }
- public static void main(String args[]){
- List<String> result = findCombo();
- for(String str : result){
- System.out.println(str);
- }
- System.out.println("Total combinations: "+result.size());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement