We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I wanted to share a Java solution for checking if two strings are anagrams of each other. Anagrams are strings that contain the same characters in the same frequencies.
import java.util.Scanner;
public class Solution {
// Method to determine if two strings are anagrams
static boolean isAnagram(String a, String b) {
// Convert both strings to uppercase for case-insensitive comparison
a = a.toUpperCase();
b = b.toUpperCase();
// Check if lengths are different; if so, they cannot be anagrams
if (a.length() != b.length()) {
return false;
} else {
// Convert strings to character arrays
char A[] = a.toCharArray();
char B[] = b.toCharArray();
// Sort character arrays
java.util.Arrays.sort(A);
java.util.Arrays.sort(B);
// Compare sorted arrays
return java.util.Arrays.equals(A, B);
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Read two input strings
String a = scan.next();
String b = scan.next();
scan.close();
// Check if strings are anagrams
boolean result = isAnagram(a, b);
// Print result
System.out.println(result ? "Anagrams" : "Not Anagrams");
}
}
Explanation:
isAnagram Method: This function checks if two input strings (a and b) are anagrams by:
Converting both strings to uppercase to ensure case insensitivity.
Comparing their lengths; if different, they cannot be anagrams.
Converting each string into character arrays, sorting them, and comparing the sorted arrays.
main Method: Reads two input strings, calls isAnagram to check if they are anagrams, and prints "Anagrams" if true, otherwise "Not Anagrams".
This approach assumes alphanumeric inputs and ignores case sensitivity for simplicity. Feel free to ask questions if you have any doubts or need further explanation!
Happy coding! 😊
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Anagrams
You are viewing a single comment's thread. Return to all comments →
Hello everyone,
I wanted to share a Java solution for checking if two strings are anagrams of each other. Anagrams are strings that contain the same characters in the same frequencies.
import java.util.Scanner;
public class Solution { // Method to determine if two strings are anagrams static boolean isAnagram(String a, String b) { // Convert both strings to uppercase for case-insensitive comparison a = a.toUpperCase(); b = b.toUpperCase();
} Explanation: isAnagram Method: This function checks if two input strings (a and b) are anagrams by: Converting both strings to uppercase to ensure case insensitivity. Comparing their lengths; if different, they cannot be anagrams. Converting each string into character arrays, sorting them, and comparing the sorted arrays. main Method: Reads two input strings, calls isAnagram to check if they are anagrams, and prints "Anagrams" if true, otherwise "Not Anagrams". This approach assumes alphanumeric inputs and ignores case sensitivity for simplicity. Feel free to ask questions if you have any doubts or need further explanation!
Happy coding! 😊