Rajiur Rahman
BAN USERWhat actually is being done by this code? Are you sure you posted the right segment?
- Rajiur Rahman October 25, 2015Here is one simple solution.
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int input = in.nextInt();
ArrayList<Integer> seedNumbersList = new ArrayList<Integer>();
for(int i=0; i<input/2; i++){
char[] numStr = Integer.toString(i).toCharArray();
int temp = i;
for(char c:numStr){
temp *= Character.getNumericValue(c);
}
if(temp == input){
seedNumbersList.add(i);
}
}
for(int number:seedNumbersList){
System.out.println(number);
}
}
Here is my version
public class KeyPad {
public static void main(String[] args){
HashMap<Character, List<Character>> numPad = BuildHashMap();
Scanner in = new Scanner(System.in);
String input = in.nextLine();
String[] tokens = input.split("#");
for(String tok: tokens){
if(tok.length() > 1){ //user has either pressed different keypands or pressed same keypad more than once
int count = 0;
Character cCurrent, cNext;
for(int i=0; i<tok.length()-1; i++){
cCurrent = tok.charAt(i);
cNext = tok.charAt(i+1);
if(cCurrent == cNext){ //same key pressed twice or more
count ++;
if(count == numPad.get(cCurrent).size()-1 ){
//this case if a particular key has been pressed more than 3/4 times. This print is for rolling over
System.out.println(numPad.get(cCurrent).get(count+1));
count = 0;
}
}
else{ //there are difference between current and next key
//either we have seen same number before or not. 'count' variable will tell the story
if(count == 0){//keys are different, print the Character
System.out.print(numPad.get(cCurrent).get(0));
}
else{
System.out.print(numPad.get(cCurrent).get(count));
count = 0;
}
}
}
//Deal with the last character of "tok" separately since the for loop ends before processing it
if(count == 0){
System.out.print(numPad.get(tok.charAt(tok.length()-1)).get(0));
}
else{
System.out.print(numPad.get(tok.charAt(tok.length()-1)).get(count));
}
}
else{ //if the length of the tok is only 1. one number followed by #
System.out.print(numPad.get(tok.charAt(0)).get(0));
}
System.out.print(" "); //print the blank space for hash
}
}
private static HashMap<Character, List<Character>> BuildHashMap() {
HashMap<Character, List<Character>> numPad = new HashMap<Character, List<Character>>();
numPad.put('1', new ArrayList<>());
numPad.put('2', Arrays.asList('A', 'B', 'C'));
numPad.put('3', Arrays.asList( 'D', 'E', 'F' ) );
numPad.put('4', Arrays.asList( 'G', 'H', 'I' ) );
numPad.put('5', Arrays.asList( 'J', 'K', 'L' ) );
numPad.put('6', Arrays.asList( 'M', 'N', 'O' ) );
numPad.put('7', Arrays.asList( 'P', 'Q', 'R', 'S' ) );
numPad.put('8', Arrays.asList( 'T', 'U', 'V' ) );
numPad.put('9', Arrays.asList( 'W','X', 'Y', 'Z' ) );
numPad.put('0', new ArrayList<Character>() );
return numPad;
}
}
Well, the problem seems pretty complicated to me. How about an easy solution like following?
- Rajiur Rahman October 25, 2015- Find all the permutations
- Filter out the numbers matching constraints