A Recursive soln:
NUM_PER_DIGIT = 3
char c[][NUM_PER_DIGIT] = {"abc","def",...};
char n[] = 2156169 (number is pressed);
int k=7;
char * s = (char *) malloc(sizeof(char) * k);
void printAllCombinations (char c[][], char n[], int k, char *s, int count)
{
if (count >= k - 1) {
s[k] = '\0';
print (s);
} else {
for (i =0; i< NUM_PER_DIGIT; i++) {
s[i] = c[count][i];
printAllCombinations(c,n,k,s, count+1);
}
}
}
printAllCombinations (c,n,k,s,0);
Please correct me if my understanding is wrong.
Thanks,
Immanuel
On Mon, May 23, 2011 at 9:33 PM, immanuel kingston <
[email protected]> wrote:
> Extending the above soln:
>
> NUM_PER_DIGIT = 3
> char c[][NUM_PER_DIGIT] = {"abc","def",...};
>
> char n[] = 2156169 (number is pressed);
> int k=7;
>
> for i <-- 0 to NUM_PER_DIGIT ^ k
> String s="";
> for j <-- 0 to k
> int index = getJthDigitinItotheBaseNumPerDigit(NUM_PER_DIGIT,i,j);
> // ie get 1st digit in (022)3 returns 2
> s += c[n[j]][index];
> print(s);
>
>
> Time Complexity: O((NUM_PER_DIGIT^k)*k^2);
>
>
> On Mon, May 23, 2011 at 7:32 PM, anshu mishra
> <[email protected]>wrote:
>
>> the same question i have asked in microsoft interview. (if it is the same
>> :P)
>>
>> for 12 perutation are (ad, ae, af, bd, be, bf, cd, ce ,cf);
>> i have given them 3 solution(recusrsive, stack based) and the last one
>> what they wanted.
>>
>> take a tertiary number(n) = 3^(number of digits) in case of 12 it is
>> equals to 2;
>>
>> i m solving the save problem. assuming on every key there are only 2
>> alphabets.
>>
>> char c[][2] = {ab , cd, ......};
>>
>> char n[] = 2156169 (number is pressed);
>> so k = 7;
>> for (i = 0; i < (1<<k); i++){
>> string s = "";
>> for (j = 0; j < k; j++){
>> s += c[n[j]][i & (1<<j)]
>> }
>> print(s);
>> }
>>
>> same logic u can use for tertiary too.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.