Java Anagrams Solution
Problem Overview
Original Problem: Java Anagrams | Hackerrank
Difficulty: Easy
Category: String Manipulation
Description: We are given two strings, and our task is to determine if the two strings are anagrams of each other.
Understanding the Problem
To determine if the strings are anagrams, we need to check whether each string contains the same characters with the same frequencies, regardless of order or case.
Input:
Two strings containing only English alphabetic characters.
Output:
Return true if the strings are anagrams of each other, otherwise false.
Initial Reasoning
The original problem gives us template code to read the strings and does not allow us to import any libraries. To write the isAnagram method, we can use a fixed-size array to store the frequencies of each character in each string after they have been converted to lowercase. In this case, we can use a size of 256 for extended ASCII characters, and when we compare the arrays, if the character frequencies are the same, then the strings are anagrams.
Pseudocode
// isAnagram method:
Convert strings to lowercase
If the strings are not the same length
Return false
Create an array for each string
For each character in the first string
Update its frequency in the array
For each character in the second string
Update its frequency in the array
For each possible ASCII character
If the frequencies in each array do not match
Return false
Return true
Solution Code
// Note this is one of many possible solutions
import java.util.Scanner;
public class Solution {
static boolean isAnagram(String firstString, String secondString) {
// Convert both strings to lowercase
firstString = firstString.toLowerCase();
secondString = secondString.toLowerCase();
// If lengths are not equal, they cannot be anagrams
if (firstString.length() != secondString.length()) {
return false;
}
// Create arrays to hold character frequencies
int[] freqfirstString = new int[256];
int[] freqsecondString = new int[256];
// Count character frequencies for the first string
for (int i = 0; i < firstString.length(); i++) {
freqfirstString[firstString.charAt(i)]++;
}
// Count character frequencies for the second string
for (int i = 0; i < secondString.length(); i++) {
freqsecondString[secondString.charAt(i)]++;
}
// Compare frequency arrays
for (int i = 0; i < 256; i++) {
// A single occurrence of a mismatch means they're not anagrams
if (freqfirstString[i] != freqsecondString[i]) {
return false;
}
}
// If frequencies match, the strings are anagrams
return true;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String firstString = scan.next();
String secondString = scan.next();
scan.close();
boolean result = isAnagram(firstString, secondString);
System.out.println(result ? "Anagrams" : "Not Anagrams");
}
}
Commenting on Alterations
While we have done our best to maintain similarity to the template code provided in the original problem, the following changes have been made to follow best practices:
Renamed variables:
Using more descriptive names improves the clarity of the code and makes it easier for anyone reading the code to understand what each variable represents.