Thursday, December 22, 2011

Strings are Anagrams or NOT


Write a C function for detecting anagrams. Returns true if both strings are anagrams, returns false otherwise.


Alternate Solution :Use Bit Vector if repetition is not allowed
Use some bit vector and increment the specific bit corresponding to the character in first go.And decrement the relevant bit in 2nd go.

Variation 1: If a String Contains an Anagram of Another String
How to check if a string contains an anagram of another string?
string “coding interview questions” contains an anagram of string “weivretni”, however not for “abcde”.

Variation 2: Display all of the anagrams within the array of strings
Given a sequence of words, print all anagrams together
Given an array of words, print all anagrams together. For example, if the given array is {“cat”, “dog”, “tac”, “god”, “act”}, then output may be “cat tac act dog god”.

Sorting:
Trie:
Hashing:

Or

The basic idea is to come up with simple hash or signature for each string in the input string array. This signature is nothing but the characters in the string sorted alphabetically. So, the signature for "cat" will be "act" and for "tac" it will be "act" too. As you can see the signature is the same for 2 strings that are anagrams. We can use this property to identify and collect the anagrams in the inputString array together. We can use a hash table for fast insert and retrieve these anagrams, but will cost us extra memory. 

Note: There are other solutions that don't use a hashtable, but involves CPU cost is higher and involves sorting of the signature. I will post this alternative solution soon. If you have a better method, please write a comment.

Pseudo-Code: 

Loop for each string (S) in the array:

Get signature (K) for S (sort the characters of S to get its signature (K))
Insert S in hash table with signature K as the key and append S to the array list contained in the value of the hash table key.
If K already exists, don't add a new key (since it will cause collision), but just append S to the end of the list in that hash table slot.
Goto step 1 with next available string.
In the end just print all the keys in the hash table along with the list of strings in each slot.



1 comment: