GeeksforGeeks » Interview Questions
Microsoft Interview Question for Software Engineer/Developer (Fresher) about Data Structure
(2 posts)-
Design a data structure which will support the following operation : given a seven letter word, return all valid words which are anagrams of this word. He wanted a data structure which would be optimized for space, and another one optimized for time.
Write the code to implement the aforementioned function given the data structure. -
As the string contains 7 characters,total number of anagrams would be 7!.The recursive program may be like this.
#include <stdio.h>
#define SIZE 7void Anagram(int depth,char *Orig,int *track,char *Permut)
{
int i,j;
if(depth==SIZE)
{
for(j=0;j<SIZE;j++)
printf("%c",Permut[j]);
printf("\n");
}
else
{
for(i=0;i<SIZE;i++)
{
if(!track[i])
{
track[i]=1; //to indicate that ith character has been taken
Permut[depth]=Orig[i];
Anagram(depth+1,Orig,track,Permut);
track[i]=0;
}
}
}
}int main()
{
char Original[SIZE],Permutation[SIZE];
int track[SIZE]={0},n; //track[] keeps track of the characters that have already been taken.
printf("Enter characters \n");\
for(n=0;n<SIZE;n++)
scanf("%c",&Original[n]);Anagram(0,Original,track,Permutation);
return 0;
}if the characters are repeated,it assume them different.
correct me if mistaken.
Reply
You must log in to post.