coderolls Staff
coderolls Staff Hi 👋🏼, At coderolls we write tutorials about Java programming language and related technologies.

Java Program To Check If Two Strings are Anagram

Here, we will see a Java program to check if two strings are anagrams.

If two Strings have the same set of characters, then these are anagram Strings.

Let’s see a Java program to check two Strings are anagrams.

Java Program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.Arrays;

/**
 * Java program to check if the two strings are anagrams or not
 * By coderolls.com
 */
public class AnagramStrings {

    public static void main(String[] args) {

        String str1 = "race";
        String str2 = "care";

        System.out.println("Comparing str1 and str2 if they are anagram strings: ");
        boolean result1 = isAnagram(str1, str2);
        if (result1) {
            System.out.println("The two strings " + str1 + " and "+ str2 + " are anagram strings");
        } else {
            System.out.println("The two strings '" + str1 + "' and '"+ str2 + "'  are not anagram strings");
        }
    }

    private static boolean isAnagram(String string1, String string2) {
        //String with different lengths can't be an anagram string
        if (string1.length() != string2.length()) {
            return false;
        }
        char[] arr1 = string1.toCharArray();
        char[] arr2 = string2.toCharArray();
        Arrays.sort(arr1);
        Arrays.sort(arr2);
        return Arrays.equals(arr1, arr2);
    }

}

Output

1
2
Comparing str1 and str2 if they are anagram strings: 
The two strings race and care are anagram strings
Join Newsletter
Get the latest tutorials right in your inbox. We never spam!

comments powered by Disqus