An anagram is a fun little concept in programming that checks whether two strings are having the same characters, just arranged differently. (Anagrams) Imagine you’re trying to see if two words, like “listen” and “silent,” are actually just the same letters scrambled up . That’s what checking for an anagram is all about.
Let’s say you have two strings and you want to know if they’re anagrams. Here’s a simple way to think about it: If you take the letters from one string and rearrange them, can you form the other string? If the answer is yes, they’re anagrams.
Here’s how you can do it in Java:
import java.util.Arrays;
public class AnagramCheck {
public static boolean areAnagrams(String str1, String str2) {
// Remove any spaces and convert to lower case
str1 = str1.replaceAll("\\s", "").toLowerCase();
str2 = str2.replaceAll("\\s", "").toLowerCase();
// If lengths differ, they cannot be anagrams
if (str1.length() != str2.length()) {
return false;
}
// Convert strings to char arrays and sort them
char[] array1 = str1.toCharArray();
char[] array2 = str2.toCharArray();
Arrays.sort(array1);
Arrays.sort(array2);
// Compare sorted arrays
return Arrays.equals(array1, array2);
}
public static void main(String[] args) {
String str1 = "Listen";
String str2 = "Silent";
if (areAnagrams(str1, str2)) {
System.out.println(str1 + " and " + str2 + " are anagrams.");
} else {
System.out.println(str1 + " and " + str2 + " are not anagrams.");
}
}
}
To check if two strings are anagrams, the code does a few simple things. First, it gets rid of any spaces and turns everything into lowercase, so we can compare fairly “Listen” and “Silent”. Then, it converts each string into an array of characters, sorts them, and finally checks if they are the same.
If they match, it means both strings contain the same characters in the same quantities, just jumbled differently. The output for “Listen” and “Silent” would be:
Listen and Silent are anagrams.
One other approach to check if two strings are anagrams is by counting the frequency of each character. If both strings have the same character counts for every letter, then they are anagrams.
import java.util.HashMap;
public class AnagramCheck {
public static boolean areAnagrams(String str1, String str2) {
// Remove spaces and convert to lower case
str1 = str1.replaceAll("\\s", "").toLowerCase();
str2 = str2.replaceAll("\\s", "").toLowerCase();
// If lengths differ, they cannot be anagrams
if (str1.length() != str2.length()) {
return false;
}
// Create a frequency map for characters in the first string
HashMap<Character, Integer> charCountMap = new HashMap<>();
for (char c : str1.toCharArray()) {
charCountMap.put(c, charCountMap.getOrDefault(c, 0) + 1);
}
// Decrease the count for characters in the second string
for (char c : str2.toCharArray()) {
if (!charCountMap.containsKey(c)) {
return false; // Character not found in the first string
}
charCountMap.put(c, charCountMap.get(c) - 1);
if (charCountMap.get(c) == 0) {
charCountMap.remove(c); // Remove character when its count reaches 0
}
}
// If map is empty, strings are anagrams
return charCountMap.isEmpty();
}
public static void main(String[] args) {
String str1 = "Listen";
String str2 = "Silent";
if (areAnagrams(str1, str2)) {
System.out.println(str1 + " and " + str2 + " are anagrams.");
} else {
System.out.println(str1 + " and " + str2 + " are not anagrams.");
}
}
}
We have just seen a neat little trick that comes in handy, especially when you’re dealing with puzzles, games, or even just curious wordplay!
Leave a Reply