National Informatics Centre Interview Question
Scientific OfficersCountry: India
Interview Type: Phone Interview
Step 1:store the first string in p,
Step 2: store the second string in s,
Step 3: create a array(occurrence[26]) and initialise it with zero,
Step 4:find occurrence of leters in first string ,second string and increase the corresponding index in occurrence[26] array corresponding the 1st string and same time decrease the occurrence[26] corresponding the 2nd string ,{if p[i]==b the p[i]-‘a’ will be 1}
Step 5 :If all the contents of occurrence[26] array is zero then both the string have same set of letter otherwise different letter .
Any problem cotact ansariinjnu@gmail.com
#include<stdio.h>
Int main()
{
Char p[10],s[10];
Int i=0,occurrence[26]={0};
Printf(“\nEnter the 1st string”);
Gets(p);
Printf(“\nEnter the 2nd string”);
Gets(s);
While(*p && *s)
{
Occurrence[p[i]-‘a’]++;
Occurrence[s[i++]-‘a’]--;
}
If(*p || *s)
{
Printf(“Both string is not same:”);
Return;
}
I=0;
While( i<26 && ! occurrence[i])
I++;
If(i==26)
Printf(“Bothe string is same”);
Else
Printf(“Bothe string is not same:”);
Retirn 0;
}
This function will return 1 if Equal otherwise it will return 0.
int strMatch(char str1[],char str2[]){
int arr[256];
for(int i=0;i<256;i++){
arr[i]=0;
}
for(int i=0;str1[i]!='\0';i++){
arr[str1[i]]++;
}
for(int i=0;str2[i]!='\0';i++){
if(arr[str2[i]]<=0) return 0;
arr[str2[i]]--;
}
for(int i=0;i<256;i++){
if(arr[i]>0) return 0;
}
return 1;
}
Sort and Compare O(nlogn)
- Loler October 01, 2012