Valid Anagram

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

Example 1:

Input: s = “anagram”, t = “nagaram”

Output: true

Example 2:

Input: s = “rat”, t = “car”

Output: false

Approach:

To determine if two strings, s and t, are anagrams, we need to ensure the following two conditions:

  1. Equal Length:
    Both strings must have the same length. If they don’t, they can’t possibly be anagrams, and we can immediately return false.
  2. Character Frequency Match:
    After confirming the strings are of equal length, we need to check whether both strings contain the exact same characters with the same frequency. The order of characters doesn’t matter, but each character must appear the same number of times in both strings.

Initial Length Check:
Before performing any further operations, we first check if the lengths of both strings are equal. If the lengths differ, we can immediately return false because two strings of different lengths cannot be anagrams.

Count Frequency of Each Character:

  • We will use a counting array (or hash map) to track the frequency of each character in both strings.
  • Since the problem specifies the strings consist of lowercase English letters (a-z), we can use an array of size 26 to represent the count of each letter (one index for each letter from 'a' to 'z').

Update Frequency Based on First String (s):

  • We iterate over the characters in string s and increment the count for each character. This keeps track of how many times each character appears in s.

Update Frequency Based on Second String (t):

  • We then iterate over the characters in string t and decrement the count for each character. This ensures that if both strings are anagrams, the increments and decrements will cancel each other out.

Final Check of Frequencies:

  • After processing both strings, we check the frequency count array. If all values are zero, it means both strings contain exactly the same characters with the same frequency. In this case, the strings are anagrams, and we return true.
  • If any value is non-zero, it means the two strings are not anagrams, and we return false.
  • Hashmap
  • Hashtable
Java
public class Solution {
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }

        HashMap<Character, Integer> countS = new HashMap<>();
        HashMap<Character, Integer> countT = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            countS.put(s.charAt(i), countS.getOrDefault(s.charAt(i), 0) + 1);
            countT.put(t.charAt(i), countT.getOrDefault(t.charAt(i), 0) + 1);
        }
        return countS.equals(countT);
    }
}

Java
class Solution {
    public boolean isAnagram(String s, String t) {
        if(s.length() != t.length()){
            return false;
        }
        int[] count = new int[26];

        for(int i = 0; i < s.length(); i++){
            count[s.charAt(i) - 'a']++;
            count[t.charAt(i) - 'a']--;
        }

        for (int c: count){
            if (c != 0){
                return false;
            }
        }

        return true;
    }
}
Previous Article

Contains Duplicate

Next Article

Two Sum

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨