Understanding the Problem:
The problem asks how to find out if two strings are anagrams.
What is an Anagram?
- An anagram is when two strings have the same characters in the same frequency but arranged differently.
- For example:
"listen"
and"silent"
are anagrams."triangle"
and"integral"
are also anagrams.
Step-by-Step Explanation:
- Step 1: Check the Length
- First, check if both strings are of the same length. If they are not, they cannot be anagrams. For example,
"apple"
and"apples"
are not anagrams because their lengths are different.
- Step 2: Sort and Compare
- If the strings have the same length, sort both strings alphabetically and compare them. If the sorted strings are the same, the two strings are anagrams.
- Step 3: Counting Frequency of Each Character
- Another way is to count the number of times each character appears in both strings. If each character appears the same number of times in both, they are anagrams.
Example
Let’s take two strings "listen"
and "silent"
:
- Both have the same length (6 letters).
- Sorting the strings gives:
"listen"
becomes"eilnst"
"silent"
becomes"eilnst"
Since the sorted versions are identical, "listen"
and "silent"
are anagrams.
Multi-language Code Implementation
Here’s how you can implement the solution in multiple languages.
Python:
def are_anagrams(str1, str2):
# Step 1: Check if lengths are the same
if len(str1) != len(str2):
return False
# Step 2: Sort both strings and compare
return sorted(str1) == sorted(str2)
# Example usage:
print(are_anagrams("listen", "silent")) # Output: True
print(are_anagrams("apple", "apples")) # Output: False
Java:
import java.util.Arrays;
public class AnagramCheck {
public static boolean areAnagrams(String str1, String str2) {
// Step 1: Check if lengths are the same
if (str1.length() != str2.length()) {
return false;
}
// Step 2: Convert strings to char arrays, sort, and compare
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
Arrays.sort(charArray1);
Arrays.sort(charArray2);
return Arrays.equals(charArray1, charArray2);
}
public static void main(String[] args) {
System.out.println(areAnagrams("listen", "silent")); // Output: true
System.out.println(areAnagrams("apple", "apples")); // Output: false
}
}
JavaScript:
function areAnagrams(str1, str2) {
// Step 1: Check if lengths are the same
if (str1.length !== str2.length) {
return false;
}
// Step 2: Sort both strings and compare
return str1.split('').sort().join('') === str2.split('').sort().join('');
}
// Example usage:
console.log(areAnagrams("listen", "silent")); // Output: true
console.log(areAnagrams("apple", "apples")); // Output: false
C++:
#include <iostream>
#include <algorithm>
bool areAnagrams(std::string str1, std::string str2) {
// Step 1: Check if lengths are the same
if (str1.length() != str2.length()) {
return false;
}
// Step 2: Sort both strings and compare
std::sort(str1.begin(), str1.end());
std::sort(str2.begin(), str2.end());
return str1 == str2;
}
int main() {
std::cout << areAnagrams("listen", "silent") << std::endl; // Output: 1 (true)
std::cout << areAnagrams("apple", "apples") << std::endl; // Output: 0 (false)
return 0;
}
Breaking Down the Code:
- Step 1: Length Check:
- The code first checks if both strings are of the same length.
- Step 2: Sorting and Comparing:
- The code sorts both strings and compares them. If the sorted strings are the same, the two are anagrams.