
Problem Statement
Given two strings, s
and t
, the task is to determine whether the string t
is an anagram of string s
. An anagram is characterized as a rearrangement of the letters of one string to form another. Therefore, if t
can rearrange its letters to match exactly the letters and their counts in s
, the function should return true
. If not, it returns false
.
Examples
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 * 104
s
andt
consist of lowercase English letters.
Approach and Intuition
The problem is essentially about checking if two strings have the identical character counts for each type of letter, making them anagrams of each other. Here's the approach based on the example problems and constraints provided:
- If the lengths of
s
andt
are different, immediately returnfalse
because two strings of different lengths cannot be anagrams. - Count the frequency of each letter in both
s
andt
. - Compare the frequency counts:
- If they match for every letter, return
true
. - If there's a discrepancy in counts for any letter, return
false
.
- If they match for every letter, return
Example walk-through based on provided examples:
- For
s = "anagram"
andt = "nagaram"
, both strings sort to"aaagmnr"
, hence they are anagrams of each other, which means the returned value istrue
. - For
s = "rat"
andt = "car"
, sorting them gives"art"
and"acr"
respectively, which aren't identical, making them not anagrams, so the return value isfalse
.
Note that both strings consist of only lowercase English letters, and this uniformity simplifies the approach since we do not need to handle cases for uppercase letters or other characters. The check process scales with the maximum possible lengths of s
and t
, up to 50,000 characters, so an efficient counting method is crucial for performance.
Solutions
- Java
public boolean checkAnagram(String str1, String str2) {
if (str1.length() != str2.length()) {
return false;
}
int[] count = new int[26];
for (int i = 0; i < str1.length(); i++) {
count[str1.charAt(i) - 'a']++;
}
for (int i = 0; i < str2.length(); i++) {
count[str2.charAt(i) - 'a']--;
if (count[str2.charAt(i) - 'a'] < 0) {
return false;
}
}
return true;
}
The provided Java method, checkAnagram
, determines if two strings (str1
and str2
) are anagrams of each other. Anagrams are words or phrases that contain the same characters in the same frequency but in a different order.
Understand the workflow of the method:
Initially, check if both strings are of equal length. If they are not, return
false
as they cannot be anagrams.Use an array,
count
, of size 26 (representing the 26 lowercase English letters) to count the frequency of each character instr1
.Increase the respective index in the
count
array for each character instr1
.Then, decrement the count for each character found in
str2
. If any character's count goes below zero during this step, immediately returnfalse
. This check ensures thatstr2
does not have more of a particular character thanstr1
.If all counts are balanced, return
true
indicating the strings are anagrams.
This approach efficiently verifies anagram status using a frequency counting method, ensuring that both space and time complexities are kept to O(n), where n is the length of the strings.
No comments yet.