2. Valid Anagram

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

Examples

Example 1:

Input: s = "anagram", t = "nagaram"

Output: true

Example 2:

Input: s = "rat", t = "car"

Output: false

Code

class Solution {
    public boolean isAnagram(String s, String t) {
        char[] firstStringArray = s.toCharArray();
        char[] secondStringArray = t.toCharArray();
        if (firstStringArray.length != secondStringArray.length) {
            return false;
        }
        HashMap<String, Integer> stringHolderHashmap = new HashMap<String, Integer>();
        for (int index = 0; index < firstStringArray.length; index++) {
            if (stringHolderHashmap.containsKey(String.valueOf(firstStringArray[index]))) {
                int value = stringHolderHashmap.get(String.valueOf(firstStringArray[index]));
                value += 1;
                stringHolderHashmap.put(String.valueOf(firstStringArray[index]), value);
            } else {
                stringHolderHashmap.put(String.valueOf(firstStringArray[index]), 1);
            }
        }
        for (int index = 0; index < secondStringArray.length; index++) {
            if (stringHolderHashmap.containsKey(String.valueOf(secondStringArray[index]))) {
                int value = stringHolderHashmap.get(String.valueOf(secondStringArray[index]));
                value -= 1;
                stringHolderHashmap.put(String.valueOf(secondStringArray[index]), value);
            }
        }
        for(String key : stringHolderHashmap.keySet()) {
            int value = stringHolderHashmap.get(key);
            if (value != 0) {
                return false;
            }
        }
        return true;
    }
}

Overall, the approach uses a HashMap to count the occurrences of each character in the first string and then decrements those counts based on the characters in the second string. If all counts are zero at the end, the two strings are anagrams.

  • The method converts both strings into character arrays (firstStringArray and secondStringArray) for easier manipulation.

  • If the lengths of the two arrays are not equal, the method immediately returns false, since anagrams must have the same number of characters.

  • A HashMap named stringHolderHashmap is initialized to keep track of character counts. The method iterates through firstStringArray and counts occurrences of each character:

    • If the character already exists in the map, its count is incremented.

    • If it doesn’t exist, it is added to the map with a count of 1.

  • The method then iterates through secondStringArray and decreases the counts of each character in the stringHolderHashmap:

    • If the character from secondStringArray exists in the map, its count is decremented by 1.

  • Finally, the method checks if all counts in the stringHolderHashmap are 0. If any count is not 0, it means that the characters in the two strings do not match up completely, and the method returns false.

  • If all counts are 0, the method concludes that s and t are anagrams and returns true.

This might not be the optimal solution, but it’s a starting point for my work. As I progress, I hope to enhance my coding style along the way.

Last updated