Amazon Interview Question
Software Engineer / Developers@ss:i don think the idea is absurd...in ur example..if string 2 is divided by string 1...then it would be val(b)*val(i)/val(e)(remaining cancelled) which will return a floating point as all nos are prime.So the remainder is 0 and will return false as expected...
My solution:
1. Keep the First string's Characters to a HashSet - O(n)
2. Check for each character from Second String in the HashSet. If any character didn't match, return false - O(m)
Total Time Complexity - O(m+n)
Space Complexity -O(n)
two steps to solve:
1) sort the B string
2) Do a linear search for the characters of A strings
If you dont want to sort, solve the problem using associate hash map, and have count for each node(representing the character). do this for string A, and then take char by char from string B, and start decrementing the counts of nodes if the match occurs. After parsing the counts for nodes should be lesser than equal to 0.
Create a 26bit array. Now go through source and fill it using string B. Go through A and check if those bits are already set. That will be O(n).
of course this is assuming there are only 26 valid characters, and each character is assigned an index.
@Kishore: some minor modifications. use a array of size 256. and count the number of occurrences for each character in string B.
Ex: A[abcc] B[back], then the answer shud be false. if you use bit array this would be true so use a normal array
void print(char *mainstr, char *refstring)
{
int *refstringmap = (int*)malloc(sizeof(int)*26);//just assuming string has only alphabets
for(int i=0; *(refstring+i);i++)
{
refstringmap[*(refstring+i)] = 1;
refcount++;
}
for(int j=0;*(mainstring+j);j++)
{
if(refstringmap[*(mainstring+j)]
maincount++;
}
if(refcount == maincount)
return true;
else
return false;
I think this should work:
bool sameChars(const char* p1, const char* p2)
{
if (!p1 || !p2) return false;
int aryHit[256] = {0};
int nCharCount = 0;
int nSize = strlen(p1);
for (int i = 0; i < nSize; ++i)
{
if (aryHit[p1[i]] == 0) ++nCharCount;
++aryHit[p1[i]];
}
nSize = strlen(p2);
int nFoundCount = 0;
for (int i = 0; i < nSize; ++i)
{
if (aryHit[p2[i]] > 0)
{
--aryHit[p2[i]];
if (aryHit[p2[i]] == 0) ++nFoundCount;
if (nFoundCount == nCharCount)
{
return true;
}
}
}
return false;
}
This is just a pseudocode. It may have syntactical errors.
void fillArr(char countarr[], char* Astr)
{
while(Astr)
{
int i=0;
countarr[*Astr[i]] +=1;
}
}
ProcessStr(char* Astr, char*Bstr)
{
int i=0;
while(*Bstr+i)
{
temp = *Bstr+i;
if(countarr[temp]>0) countarr[temp]--;
i++;
}
}
Then, check the count array if it has any value for those characters in string A. If it is all 0, then it means those characters were all present in the B string and hence the function would return a true. Else, the function would return a false.
use a hash table....
for each element it has 2 counts...cnt1 and cnt2
initialize both counts with 0
now traverse both strings and put it in hash table and maintain their respective counts
after traversing both stings if cnt1>=cnt2 (in entire hash table) means that string2 is in string1....
use a hash table....
for each element it has 2 counts...cnt1 and cnt2
initialize both counts with 0
now traverse both strings and put it in hash table and maintain their respective counts
after traversing both stings if cnt1>=cnt2 (in entire hash table) means that string2 is in string1....
If string only has characters, assign all the a to z one prime number.
- Vabs February 17, 2011a = 1, b = 2, c = 3, d = 5 ...
Multiply all the characters of string 1.
For eg. abc == 1 * 2 * 3 = 6
and string 2 also.
Find remainder mul(string1) / mul(string2). If it is 0; than string 2 is in string 1, otherwise no.
(Not works for bigger inputs)