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;
}
}
Comments