Skip to main content

Command Palette

Search for a command to run...

Group Anagrams

Published
2 min read
Group Anagrams
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

Given an array of strings strs, group the anagrams together. You can return the answer in any order.

LeetCode Problem - 49

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        // Initialize the result list to store groups of anagrams
        List<List<String>> result = new ArrayList<>();

        // Iterate through each string in the input array
        for (int i = 0; i < strs.length; i++) {
            // Create a new list to store anagrams of the current string
            List<String> list = new ArrayList<>();

            // Skip strings that have already been grouped (marked with "-")
            if (!strs[i].equals("-")) {
                String outerStr = strs[i]; // The current string to find anagrams for

                // Iterate through the remaining strings to find anagrams
                for (int j = i; j < strs.length; j++) {
                    String innerStr = strs[j]; // The string being compared

                    // If the string is not marked and is an anagram of outerStr
                    if (!innerStr.equals("-") && findAnagram(outerStr, innerStr)) {
                        list.add(innerStr); // Add the anagram to the list
                        strs[j] = "-"; // Mark the string as processed
                    }
                }
            }

            // If the list contains anagrams, add it to the result list
            if (!list.isEmpty()) {
                result.add(list);
            }
        }
        // Return the result list containing groups of anagrams
        return result;
    }

    // Helper function to check if two strings are anagrams
    public boolean findAnagram(String str1, String str2) {
        // If the lengths are different, they cannot be anagrams
        if (str1.length() != str2.length()) {
            return false;
        }

        // Array to count the frequency of each character in the strings
        int[] count = new int[26];

        // Compare each character of both strings and update the count array
        for (int i = 0; i < str1.length(); i++) {
            count[str1.charAt(i) - 'a']++; // Increment the count for str1
            count[str2.charAt(i) - 'a']--; // Decrement the count for str2
        }

        // If all counts are zero, the strings are anagrams
        for (int value : count) {
            if (value != 0) {
                return false; // Not anagrams if any count is non-zero
            }
        }

        // If no mismatch in character counts, return true (they are anagrams)
        return true;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.