// Return count of all possible numbers of length n // in a given numeric keyboard int getCount(char keypad[][3], int n) { // Base cases if (keypad == NULL || n <= 0) return 0; if (n == 1) return 10; int i=0, j=0, totalCount = 0; totalCount += getCountUtil(keypad, 1, 1, n); return totalCount; for (i=0; i<4; i++) // Loop on keypad row { for (j=0; j<3; j++) // Loop on keypad column { // Process for 0 to 9 digits if (keypad[i][j] != '*' && keypad[i][j] != '#') { // Get count when number is starting from key // position (i, j) and add in count obtained so far totalCount += getCountUtil(keypad, i, j, n); printf("\n"); } } } return totalCount; }
// Returns count of numbers of length n starting from key position // (i, j) in a numeric keyboard. int getCountUtil(char keypad[][3], int i, int j, int n) { if (keypad == NULL || n <= 0) return 0; // From a given key, only one number is possible of length 1 if (n == 1) return 1; int k=0, move=0, ro=0, co=0, totalCount = 0; // move left, up, right, down from current location and if // new location is valid, then get number count of length // (n-1) from that new position and add in count obtained so far for (move=0; move<5; move++) { ro = i + row[move]; co = j + col[move]; if (ro >= 0 && ro <= 3 && co >=0 && co <= 2 && keypad[ro][co] != '*' && keypad[ro][co] != '#') { totalCount += getCountUtil(keypad, ro, co, n-1); printf("%c", keypad[ro][co]); } } return totalCount; }
//-------------------------------------------------------------------------------------------------- // Return count of all possible numbers of length num in a given numeric keyboard int getCount(char keypad[][3], int num) { if (keypad == NULL || num <= 0) // Base cases return 0; if (num == 1) return 10; // Base cases, pre calculated for Num = 1 int i = 0, j = 0, totalCount = 0; for (i = 0; i < 4; i++) // Loop on keypad row, only 4 r there { for (j = 0; j < 3; j++) // Loop on keypad column { if (keypad[i][j] != '*' && keypad[i][j] != '#') // Process for 0 to 9 digits { // - Get count when number is starting from key position (i, j) and totalCount += getCountUtil(keypad, i, j, num); // add in count obtained so far } } } return totalCount; }
//-------------------------------------------------------------------------------------------------- // Returns count of numbers of length num starting from key position (i, j) in a numeric keyboard. int getCountUtil(char keypad[][3], int R, int C, int num) { if (keypad == NULL || num <= 0) return 0; if (num == 1) // From a given key, only one number is possible of length 1 return 1; int k=0, move=0, new_R=0, new_C=0, totalCount = 0; // move left, up, right, down from current location and if new location is valid, then get number // count of length (num-1) from that new position and add in count obtained so far for (move=0; move<5; move++) { new_R = R + row[move]; new_C = C + col[move]; if ( new_R >= 0 && new_R <= 3 && new_C >=0 && new_C <= 2 && keypad[new_R][new_C] != '*' && keypad[new_R][new_C] != '#') { totalCount += getCountUtil(keypad, new_R, new_C, num - 1); } } return totalCount; }