Two strings are said to be anagram if we can form one string by arranging the characters of another string. For example, Race and Care. Here, we can form Race by arranging the characters of Care.
Example 1: Java program to check if two strings are anagrams
Output
race and care are anagram.
In Java, we have two strings named str1 and str2. We are checking if str1 and str2 are anagrams.
We first convert the strings to lowercase. It is because Java is case sensitive and R and r are two difference characters in Java.
Here,
- str1.toCharArray() - converts the string into a char array
- Arrays.sort() - sorts both the char arrays
- Arrays.equal() - checks if the sorted char array are equal
If sorted arrays are equal, then the strings are anagram.
Note: The Arrays.sort() compares two characters with ASCII value. And, characters R and r are not equal. Hence, strings should be converted to the same case.
Example 2: Take string inputs from users and check if the strings are anagram
Output
Enter first String: Race Enter second String: Care Race and Care are anagram.
In the above example, we have used the Scanner class to take input from the user. Here, we checked if the strings provided by users are anagram.