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.
- Prepare
- Java
- Strings
- Java Anagrams
- Discussions
Java Anagrams
Java Anagrams
Sort by
recency
|
2372 Discussions
|
Please Login in order to post a comment
this is complete code for this, we have to convert both string into lowercase or uppercase then we can continue arrays method
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! 😊
Here is my algorithm. Complexity O(n). Which includes 2 passes for arrays a / b. And one additional pass for a fixed size array to hold 26 letter frequencies.
All array passes are in forward direction which shall be highly optimized by CPU preloading RAM into CPU cache.
import java.util.Scanner;
public class Solution {
}