Leetcode 242 Valid Anagram Solution in java | Hindi Coding Community

0


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

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Example 1:


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

Output: true



class Solution {
    public boolean isAnagram(String s, String t) {
     
        if(s == null || t == null) return false;
 
        if(s.length() != t.length()) return false;
     
        int[] counter = new int[26];
   
        for(int idx = 0; idx < s.length(); idx++){
            counter[s.charAt(idx)-'a']++;
            counter[t.charAt(idx)-'a']--;
        }
       
        for(int idx: counter){
            if(idx != 0)
                return false;
        }
        return true;
    }
}

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !