Write a java program How to check if two strings are anagrams of each other?

Write a java program How to check if two strings are anagrams of each other?

Program
We can say if two strings are an anagram of each other if they contain the same characters but at different orders.
Example: sateesh & hseetas
import java.util.*;
public class Main
{
	public static void main(String[] args) 
	{
		String str1 = "sateesh";
        String str2 = "hseetas";
     
          
        //Checking for the length of strings  
        if (str1.length() != str2.length()) 
        {  
            System.out.println("Both the strings are not anagram");  
        }  
        else {  
            //Converting both the arrays to character array  
            char[] ch1 = str1.toCharArray();  
            char[] ch2 = str2.toCharArray();  
              
            //Sorting the arrays using in-built function sort ()  
            Arrays.sort(ch1);  
            Arrays.sort(ch2);  
              
            //Comparing both the arrays using in-built function equals ()  
            if(Arrays.equals(ch1, ch2) == true) {  
                System.out.println("Both the strings are anagram");  
            }  
            else {  
                System.out.println("Both the strings are not anagram");  
            }  
        }  
        
	}

}
	
	

Output:

Both the strings are anagram





More Questions


14 . Write a Java-program to print unique(distinct (or non-repeating characters) ) characters from the string ?
15 . Write a java program How to check if two strings are anagrams of each other?
16 . How to reverse a string in Java without using the reverse method ?
17 . How to count the occurrence of the given character in a string?
18 . How to check if a string is a palindrome?
19 . How to count the number of words in a given string sentence?
20 . How to reverse the words from the given string sentence?
21 . How to swap two strings without using the third variable?