Description
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
Constraints:
1 <= s.length, t.length <= 5 * 104sandtconsist of lowercase English letters.
Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
Approach 1
- Two unordered count map and a final loop to check if counts are same in both
Time: O(n) Space: O(n)
class Solution {
public:
bool isAnagram(string s, string t) {
if(s.size() != t.size()) {
return false;
}
unordered_map<char,int> countS;
unordered_map<char,int> countT;
for(int i = 0; i < s.size(); i++) {
countS[s[i]]++;
countT[t[i]]++;
}
for(const auto& pair: countS) {
if(countS[pair.first] != countT[pair.first]) {
return false;
}
}
return true;
}
};Approach 2
- Create an integer array of size 26
- Loop through string and add 1 to array if it is in first string and -1 for second
- Loop through the array and if it is not zero for any key then it is not anagram
class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) {
return false;
}
int[] help = new int[26];
for(int i = 0; i < s.length(); i++) {
help[s.charAt(i) - 'a']++;
help[t.charAt(i) - 'a']--;
}
for(int n: help) {
if(n != 0) {
return false;
}
}
return true;
}
}- The number 256 represents the total number of possible character codes in the **Extended ASCII character set**.
class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) {
return false;
}
int[] count = new int[256];
for(int i = 0; i < s.length(); i++) {
count[s.charAt(i)]++;
count[t.charAt(i)]--;
}
for(int n: count) {
if(n != 0) {
return false;
}
}
return true;
}
}Approach 1: Sorting ( Time, Space)
Intuition
If two strings are anagrams, sorting their characters alphabetically will produce identical arrays. Convert both strings to character arrays, sort them, and check if they are equal.
import java.util.Arrays;
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) return false;
char[] sArray = s.toCharArray();
char[] tArray = t.toCharArray();
Arrays.sort(sArray);
Arrays.sort(tArray);
return Arrays.equals(sArray, tArray);
}
}
Complexity
- Time Complexity: — Sorting two character arrays of length .
- Space Complexity: — Auxiliary space needed to convert strings to character arrays.
Approach 2: Frequency Counting (Most Optimized — Time, Space)
Intuition
Because both strings consist of lowercase English letters ('a' to 'z'), track character frequency using a fixed-size integer array of length 26.
- Return
falseimmediately if the lengths ofsandtdiffer. - Increment the count for each character in
swhile decrementing for each character int. - If all array values net out to zero,
tis a valid anagram ofs.
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 val : count) {
if (val != 0) return false;
}
return true;
}
}
Complexity
- Time Complexity: — Single linear pass over strings of length .
- Space Complexity: — Fixed auxiliary space for an array of size 26.
Easy Memory Rule
“Lengths must match increment for
s, decrement fortensure every frequency nets to0!”