top of page
Search

Valid Anagram

Updated: Mar 25, 2021

Given two strings s and t , write a function to determine if t is an anagram of s.



Example 1:

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

Example 2:

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

Note: You may assume the string contains only lowercase alphabets.


Solution:


class Solution {
    public boolean isAnagram(String s, String t) {
        int[] count = new int[26];
        
        if(s.length()!=t.length()) return false;
        
        for(char c: s.toCharArray()) count[c-'a']++;
        for(char d: t.toCharArray()) count[d-'a']--;
        
        for(int i:count)
        {
            if(i>0) return false;
        }
        return true;
    }
}

10 views0 comments

Recent Posts

See All

Minimum Deletions to Make Character Frequencies Unique

A string s is called good if there are no two different characters in s that have the same frequency. Given a string s, return the minimum number of characters you need to delete to make s good. The f

Smallest String With A Given Numeric Value

The numeric value of a lowercase character is defined as its position (1-indexed) in the alphabet, so the numeric value of a is 1, the numeric value of b is 2, the numeric value of c is 3, and so on.

bottom of page