Check If Two Strings Are Anagram using Java

Java programming is pretty much found on every Android smartphone today. Not only that but there are many devices/products which use Java as their main development platform. However, learning Java isn’t easy, that being said it’s not difficult either. Java is an all-rounder, so Programming in Java has many benefits. But if you are planning to write simple code, then it won’t work. To level up your programming skills you need to try solving difficult and tricky questions. Let’s start this article by playing with strings. Today we are going to check if two strings are anagram to each other or not using Java.

Reading this article only won’t do anything you need to practice these questions on your own so that you can solve them pretty easily. In the end, it’s all about increasing your critical thinking ability to solve complex problems. So open up your IDE and let’s get started. Practising these types of questions also helps to get an upper edge in Competitive Programming.

 

What is An Anagram String?

 

  • An anagram of a string is a string that consists same no of characters representing the same letters as that of the string we’re comparing with. Though anagram strings have the same character count the order in which characters are written should be different in both the strings.

 

  • For example, “abcd” and “dcba” are an anagram of each other.

 

What’s The Approach?

 

  • We will create two character arrays and will store each character of the string individually that we are going to compare in this program. We will simply use the sorting method to check if the input strings are anagrams or not.

 

 

  • However, if the length of strings is equal then we will sort both of them in ascending order using the Arrays.sort() function. After that, we will check if both the sorted strings are equal to each other or not. If yes then strings are anagrams and vice versa.

 

Also Read: How to Run Java in Visual Studio Code on Mac OS

 

Java Program To Check If Two Strings Are Anagram

 

Input:

 

char str1[] = { 't', 'e', 's', 't' }

char str2[] = { 't', 't', 'e', 'w' }

 

Output:

 

The two strings are not anagram of each other

 

 

// JAVA program to check whether two strings
// are anagrams of each other
import java.io.*;
import java.util.Arrays;
import java.util.Collections;

class TechDecodeTutorials {

    /* function to check whether two strings are
    anagram of each other */
    static boolean areAnagram(char[] str1, char[] str2)
    {
        // Get lenghts of both strings
        int n1 = str1.length;
        int n2 = str2.length;

        // If length of both strings is not same,
        // then they cannot be anagram
        if (n1 != n2)
            return false;

        // Sort both strings
        Arrays.sort(str1);
        Arrays.sort(str2);

        // Compare sorted strings
        for (int i = 0; i < n1; i++)
            if (str1[i] != str2[i])
                return false;

        return true;
    }

    /* Driver Code*/
    public static void main(String args[])
    {
        char str1[] = { 't', 'e', 's', 't' };
        char str2[] = { 't', 't', 'e', 'w' };
    
        // Function Call
        if (areAnagram(str1, str2))
            System.out.println("The two strings are"
                            + " anagram of each other");
        else
            System.out.println("The two strings are not"
                            + " anagram of each other");
    }
}


 

 

Ethix

I'm a coding geek interested in cyberspace who loves to write and read

Leave a Reply

Your email address will not be published.