Microsoft Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
1.take an array of 256 size,(for ascii charctrs),initialise with 0.
2.iterate through the string,increment the value of array by 1,on position pointd by ascii value of char obtained.
3.
do this for both string.
4.iterate through array of both strings,if same. than true else false..
time complexity o(n)
space complexity o(1){since int array is independent of string size}.
You can as well decrease the value in the first array while traversing the second string, whenever we get a negative value we know they are not permutations of each other. And in the end we have to verify if the array has all zeros. This way you will save half of the space. Moreover we do not know if only characters are allowed, and also characters could be case sensitive.
yar,how come we will recieve negative values?? nd yes there cn be integers instead of charcters,hence i hav taken array of 256 rather than 26.. pls do xplain me your concept.. i feel u r sayng right,bt nt able to get it..
Yes, your 256 array will cover the digits as well, i was mistaken in this regard.
Related to the negative values consider the example given box and obxx.
Based on the first string you get counts as 1 for b, 1 for o and 1 for x.
When we are passing through the second string we could decrease the count we got in first string.
When we first take o in obxx we decrease the count of o to 0.
When we take b in obxx we decrease the count of b to 0.
When we take first x in obxx we decrease the count of x to 0.
When we take second x (last character) in obxx we decrease the count of x to -1.
We got negative value here that means there is one x more in second string than in the first string.
We can terminate here without checking further characters if at all they exist.
bool isPermutation(string s1, string s2)
{
if(s1.length != s2.length)
return false;
int []arr = new int[26];
for(int i=0;i<s1.length;i++)
{
int index = s1[i] - 'a';
arr[index]++;
index = s2[i] - 'a';
arr[index]++;
}
for(int i=0;i<arr.length;i++)
if(arr[i] != 0)
return false;
returnt true;
}
This could also be done by traversing both the list and adding the values of each character,if the final answer is same then the strings are permutations of one another otherwise not.
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char a[]="hello", b[]="oheee";
if(strlen(a)!=strlen(b))
{
cout<<"\nNO\n";
return 0;
}
int count[53];
for(int i=0;i<52;++i)
count[i]=0;
int k,i;
k=strlen(a);
i=0;
while(i<k)
{
count[(int)a[i]-65]+=1;
count[(int)b[i]-65]-=1;
i++;
}
i=0;
while(i<52)
{
if(count[i]!=0)
{
cout<<"\nNO\n";
return 0;
}
i++;
}
cout<<"\n YeS\n";
return 0;
}
#include<string.h>
#include<conio.h>
#include<malloc.h>
#include<stdio.h>
#include<iostream.h>
#include<iostream>
void check(char *,char *);
void check(char *a,char *b)
{
if(strlen(a)!=strlen(b))
{
printf("It is not permutation");
}
else
{
int x=0,y=0;
for(int i=0;i<strlen(a);i++)
{
x=x+*(a+i);
y=y+*(b+i);
}
if(x==y)
{
printf("It is permutation");
}
else
{
printf("It is not a permutation");
}
}
}
int main()
{
char* a=(char *)malloc(100) ;
char* b=(char *)malloc(100) ;
gets(a);
gets(b);
check(a,b);
getch();
return 0;
}
Method1: traverse two strings, one from its head and the other from the end. compare every elements from both string.
Method2: first get two strings length( which will cost o(n) time); compare the lengths if the are not equal, return false; else traverse first string by increasing variable i, and compare str1[i] with str2[len-i-1] if one of them are not equal return false.
...
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 50
int main()
{
char str1[MAX],str2[MAX];
int i,j,count=0;
scanf("%s%s",str1,str2);
if(strlen(str1)!=strlen(str2))
printf("%s and %s are not permutations of each other",str1,str2);
else
{
for(i=0;i<strlen(str1);i++)
{
for(j=0;j<strlen(str2);j++)
{
if(str1[i]==str2[j])
{
count++;
break;
}}
}
if(count==strlen(str1))
printf("%s and %s are permutations of each other",str1,str2);
else
printf("%s and %s are not permutations of each other",str1,str2);
}
return 0;
}
#include<stdio.h>
#include<string.h>
int main()
{
char str1[10000];
char str2[10000];
scanf("%s",str1);
scanf("%s",str2);
int i,k1,k2,sum1=0,sum2=0;
k1=strlen(str1);
k2=strlen(str2);
if(k1!=k2)
printf("FALSE");
else{
for(i=0;i<k1;i++)
{
sum1+=str1[i];
sum2+=str2[i];
}
if(sum1==sum2)
printf("TRUE");
else
printf("FALSE");
}
return 0;
}
- hao.liu0708 August 27, 2012