Java
Java

 

Program Code


import java.util.*;

public class AnagramGrouper {
    
    /**
     * Groups anagrams from a list of strings.
     *
     * @param strs List of strings to group into anagrams.
     * @return A list of lists, where each sublist contains anagrams.
     */
    public List<List> groupAnagrams(String[] strs) {
        Map<String, List> anagramMap = new HashMap<>();
        
        for (String str : strs) {
            // Sort the characters of the string to generate a key
            char[] charArray = str.toCharArray();
            Arrays.sort(charArray);
            String key = new String(charArray);
            
            // Grouping anagrams using the sorted string as key
            anagramMap.putIfAbsent(key, new ArrayList<>());
            anagramMap.get(key).add(str);
        }
        
        // Returning the values as a list of lists
        return new ArrayList<>(anagramMap.values());
    }

    public static void main(String[] args) {
        AnagramGrouper ag = new AnagramGrouper();
        String[] input = {"eat", "tea", "tan", "ate", "nat", "bat"};
        
        List<List> groupedAnagrams = ag.groupAnagrams(input);
        System.out.println(groupedAnagrams);
    }
}
        

Program Structure Explanation

The program is structured around a class named AnagramGrouper, which contains a method called groupAnagrams.

  1. Imports:
    The program imports the necessary classes from the Java standard library: java.util.* for collections.
  2. groupAnagrams Method:
    • This method takes an array of strings as input and returns a list of lists containing grouped anagrams.
    • A HashMap is used to store the sorted string as the key and a list of corresponding anagrams as the value.
    • For each string, its characters are sorted to form a key. The original string is then added to the appropriate list in the map.
    • Finally, the method returns the values of the map as a list of lists, containing the grouped anagrams.
  3. main Method:
    This method serves as the entry point for the program. It creates an instance of AnagramGrouper, defines a sample array of strings, and invokes the groupAnagrams method.

Execution

When executed, the program will group the provided strings into lists of anagrams and print the result to the console.

Example Output


        [[bat], [nat, tan], [ate, eat, tea]]
        

Conclusion

This program efficiently groups anagrams by leveraging sorting and hashing, making it both intuitive and performant.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)