-2

Given a string value of arbitrary length, you're supposed to determine the frequency of words which are anagrams of each other.

public static Map<String, Integer> generateAnagramFrequency(String str)
{ ... }

For example: if the string is "find art in a rat for cart and dna trac" your output should be a map: find -> 1 art -> 2 in -> 1 a -> 1 cart -> 2 and -> 2

The keys should be the first occurrence of the word, and the number is the number of anagrams of that word, including itself.

The solution i came up with so for is to sort all the words and compare each character from both strings till the end of either strings. It would be O(logn). I am looking for some other efficient method which doesn't change the 2 strings being compared. Thanks.

Venugopal Madathil
  • 2,031
  • 3
  • 34
  • 44

2 Answers2

1

I've written a JavaScript implementation of creation a n-gram (word analysis), at Extract keyphrases from text (1-4 word ngrams).

This function can easily be altered to analyse the frequency of anagrams:
Replace s = text[i]; by s = text[i].sort(), so that the order of characters doesn't matter any more.

Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678
1

Create a "signature" for each word by sorting its letters alphabetically. Sort the words by their signatures. Run through the sorted list in order; if the signature is the same as the previous signature, you have an anagram.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
user448810
  • 17,381
  • 4
  • 34
  • 59