Imagine you are about to receive a call from a Facebook interviewer and you are nervously biting your nails, hoping for things to go well.
The phone rings, and after a brief introduction and small talk, Seregey throws the following question your way -

Write a function to check whether two strings are anagrams of each other or not. An anagram is another string that contains the same characters in a different order. For example, “tip” and “pit” are anagrams of each other.

However, an efficient coding practice is to avoid complex processing whenever possible. A simple if condition in the above solution can make it more efficient and avoid unnecessary processing for basic cases.

Time Complexity :

In the above code, all the time needed is for the sorting to complete. Hence, the complexity depends on the type of sorting used. According to Oracle's documentation, Array.sort() is a Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm offers O(n log(n)) performance on many data sets that cause other quicksorts to degrade to quadratic performance, and is typically faster than traditional (one-pivot) Quicksort implementations.
http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(char%5B%5D)
The above solution is good. Let us see if the implementation can be made even more efficient. What if there is an additional constraint that the input string is made of only 8 bits and hence, there can be only 256 possible characters?

Hint: Array to store character counts

Create two count arrays of size 256 for each of the input strings.

Iterate through both the arrays and increment the count of characters in the corresponding arrays. If both the arrays are the same, then they are anagrams of each other.

Time Complexity:

Since we only traverse the input string once, the complexity is O(n).
Now what if you want to further reduce the space complexity? The above solution can be rewritten with just one count array! While traversing the second input string, decrement the characters from the first count array. Another constraint which could further reduce the space complexity can be that if the input string is made of only English alphabets, then we can initialize the array to a size of just 52 and the index can be found by using the formula Character.getNumericValue(str2Array[i]) - 65; for upper case letters.

To solve such questions and prepare for coding interviews - log on to firecode.io

Think you have more efficient or better solution? share it in the comments!