g0201_0300.s0242_valid_anagram.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0201_0300.s0242_valid_anagram;
// #Easy #Top_Interview_Questions #String #Hash_Table #Sorting
public class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
int[] charFreqMap = new int[26];
for (char c : s.toCharArray()) {
charFreqMap[c - 'a']++;
}
for (char c : t.toCharArray()) {
if (charFreqMap[c - 'a'] == 0) {
return false;
}
charFreqMap[c - 'a']--;
}
return true;
}
}