Given two strings, the task is to determine if they are anagrams of each other. Two strings are considered anagrams if they contain the same characters with the same frequencies, but possibly in a different order.
Input-Output Examples
Example 1:
- Input:
- String1: "listen"
- String2: "silent"
- Output: True (The strings are anagrams)
Example 2:
- Input:
- String1: "apple"
- String2: "pale"
- Output: False (The strings are not anagrams)
Approach
To check if two strings are anagrams, we need to ensure that both strings contain the same characters with the same frequencies. There are multiple ways to achieve this, but the most efficient approach involves using a hash map to count character frequencies.
Steps to implement :
- If the lengths of the two strings are different, they cannot be anagrams.
- Use a hash map to count the frequency of each character in the first string.
- Use the hash map to decrease the count for each character found in the second string.
- If all counts in the hash map are zero at the end, the strings are anagrams.
Technique to be used: Hash map for character frequency counting
C++ program to check if two strings are anagrams
cpp
#include <iostream>
#include <unordered_map>
using namespace std;
bool areAnagrams(string str1, string str2) {
if (str1.length() != str2.length()) return false;
unordered_map<char, int> charCount;
// Count characters in str1
for (char c : str1) {
charCount[c]++;
}
// Decrease count for characters in str2
for (char c : str2) {
if (charCount.find(c) == charCount.end() || charCount[c] == 0) {
return false;
}
charCount[c]--;
}
return true;
}
int main() {
string str1 = "listen";
string str2 = "silent";
if (areAnagrams(str1, str2)) {
cout << "The strings are anagrams." << endl;
} else {
cout << "The strings are not anagrams." << endl;
}
return 0;
}
Java program to check if two strings are anagrams
java
import java.util.HashMap;
import java.util.Map;
public class AnagramCheck {
public static boolean areAnagrams(String str1, String str2) {
if (str1.length() != str2.length()) return false;
Map<Character, Integer> charCount = new HashMap<>();
// Count characters in str1
for (char c : str1.toCharArray()) {
charCount.put(c, charCount.getOrDefault(c, 0) + 1);
}
// Decrease count for characters in str2
for (char c : str2.toCharArray()) {
if (!charCount.containsKey(c) || charCount.get(c) == 0) {
return false;
}
charCount.put(c, charCount.get(c) - 1);
}
return true;
}
public static void main(String[] args) {
String str1 = "listen";
String str2 = "silent";
if (areAnagrams(str1, str2)) {
System.out.println("The strings are anagrams.");
} else {
System.out.println("The strings are not anagrams.");
}
}
}
Python program to check if two strings are anagrams
python
def are_anagrams(str1, str2):
if len(str1) != len(str2):
return False
char_count = {}
# Count characters in str1
for char in str1:
char_count[char] = char_count.get(char, 0) + 1
# Decrease count for characters in str2
for char in str2:
if char not in char_count or char_count[char] == 0:
return False
char_count[char] -= 1
return True
# Example usage
str1 = "listen"
str2 = "silent"
if are_anagrams(str1, str2):
print("The strings are anagrams.")
else:
print("The strings are not anagrams.")