Pages

Create anagram buckets from a given input array of words

 Two strings are called anagrams if they contain same set of characters but in different order. For example,

  • peek and keep are anagrams

  • spar and rasp are anagrams

  • listen and silent are anagrams

For more info:

https://en.wikipedia.org/wiki/Anagram

Here we get an input array of words that contains anagram string, and we need to create buckets for all the anagrams words.

Input:

{"akka", "akak", "baab", "baba", "bbaa"}

Output:

{
    [akka, akak],
    [baab, baba, bbaa]
}

Approach

  1. Create a hashmap that will hold sorted word as the key and list of anagrams as the value. We will use this hashmap to store the results.

  2. For each word in the input array, create a key by sorting the characters and put this word to that list whose key is the sorted word. for example [aakk → akka, akak] If it does not exist then create a new list with the sorted word as key in map.

  3. In the end, traverse the values of hashmap and we get the desired result.


import java.util.*; public class Anagrams { public static void main(String[] args) { String[] input = {"akka", "akak", "baab", "baba", "bbaa"}; Map<String, List<String>> anagramsMap = anagramBuckets(input); System.out.println("anagramsMap = " + anagramsMap); } private static Map<String, List<String>> anagramBuckets(String[] input) { Map<String, List<String>> anagramsMap = new HashMap<>(100); for (String s : input) { char[] word = s.toCharArray(); Arrays.sort(word); String key = String.valueOf(word); if (!anagramsMap.containsKey(key)) { anagramsMap.put(key, new ArrayList<>()); } anagramsMap.get(key).add(s); } return anagramsMap; } }

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.