Amazon Interview Question
Software Engineer in TestsPlease check the compilation errors. The logic is to take a Map Data structure, Keys will be the characters in Strings and value will be the frequency of characters. In the first run i will populate the map and in the second run i will depopulate it. If the map is empty then the strings are empty.
public boolean checkAnagram(String a, String b)
{
if(a.length()!= b.length())
return false;
HashMap<String, Integer> hm = new HashMap<String, Integer>();
for(int i = 0; i<a.length();i++)//Sorry for not using enahanced for loop.
{
if(!(hm.containsKey(a.charAt[i])))
hm.put(a.charAt[i],1); Putting the frequency as 1 for the first time
else
{
int val = hm.get(a.charAt[i]);// Not casting to Integer as it is automatically done by Java
hm.put(a.charAt[i],++val)// Increased the frequency by 1
}
}// close the first for loop
for (int i= 0; i <b.length();i++)
{
if(hm.containsKey(b.charAt[i])
{
int val = hm.get(b.charAt[i])
if (val == 1)
hm.remove(b.charAt[i])
else
hm.put(b.charAt[i],--val)
}
}// close the 2nd for loop
return hm.isEmpty()
}// Method close
1. Store the count of each charcter in S1 into a seperate Hashtable
2. Iterate through each character of s2 and decrement the count of the corresponding key, once the count=1 remove that element. Also check if that key exists in that hastable, if not its not an anagram.
Orde is O(n)
-- Psuedo code explained on Nishank Rajvanshi Program
It's simple, I guess wats intended is that the relative distance or displacement of characters should remain the same. So the soln for this would be to append the same string in itself and now start checking if the original string is present in this String or not.
If you rearrange the letters of the word using every letter exactly once.
Basically combinatoric permutation
bool IsAnagram(char* s1, char* s2) {
char hash[256] = { 0 };
if (strlen(s1) != strlen(s2)) return false;
for(int i=0;i<strlen(s1);i++)
hash[s1[i]]++;
for(int i=0;i<strlen(s2);i++) {
if(s1[i] == s2[i] ||
--hash[s2[i]] < 0) return false;
}
return true;
}
srikanth, i didn't understand why you put the condition if(s1[i] == s2[i] || --hash[s2[i]] < 0). If you take ABCA and CBAA, first condition fails at s1[1] == s2[1] which returns false saying that both are not anagrams, which is not correct. If you take AABBCC and BCA, your condition never become false. So it returns true, which is not correct.
Reference Wiki: An anagram is .... the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once.
So ABCA and CBAA are not anagrams since letter B and last letter A are not rearranged. As mentioned in the question AABBC and CBBAA are not anagrams as third B is not changed.
And regarding AABBCC and BCA, if (strlen(s1) != strlen(s2)) return false; should takecare.
Thanks Srikanth for clarifying. I misunderstood the problem. If the interviwer put a contriant on space complexity. Is the follwing solutions works? But it is a little bit more time complexity O(nlogn)
bool IsAnagram(char* s1, char* s2)
{
int length1 = strlen(s1);
if (length1 != strlen(s2)) return false;
int length2 = length1 * 2;
s1 = strcat(s1,s2);
for(int i=0;i<length2;i++)
{
int count = 0;
for(int j=0;j<length2; j++)
{
if(s1[i] == s1[j])
{
if(j == i+length1-1) return false;
else
{
count++;
}
if(j < i) // It means jth character already counted and no need to do again, reducing time complexity
{
count = 0;
break;
}
}
}
if(count%2 != 0) return false;
}
return true;
}
1. Store the count of each charcter in S1 into a seperate
2. Iterate through each character of s2 and decrement the count of the corresponding key, once the count=1 remove that element. Also check if that key exists in that hastable, if not its not an anagram.
Orde is O(n)
-- Psuedo code explained on Nishank Rajvanshi Program
Anagrams are well defined and by that definition AABBC and CBBAA are anagrams.
- Anonymous August 20, 2011Please define the question properly.