Java Program to check if two strings are anagram

I/O Redirection in C++

What are Strings?

A string is a sequence of characters. In Java, strings are used to represent text. You can use strings to store messages, names, and other kinds of text data.
Strings are usually represented as a series of characters enclosed in quotation marks. For example, in Java, you can create a string like this:
String s = “Hello World”;

In this Article, we will write a program to check if two strings are anagram.

Strings:

We can use the various methods of the String class to manipulate strings.
For example, you can use the length() method to get the length of a string, the substring() method to get a substring of a string, and the toUpperCase() method to convert a string to uppercase. You can also use the + operator or the concat() method to concatenate two strings.

What is Anagram?

An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. For example, the word “anagram” is an anagram of the word “nagaram”.
Here are a few examples of anagrams:

  • “listen” is an anagram of “silent”
  • “dormitory” is an anagram of “dirty room”
  • “schoolmaster” is an anagram of “the classroom”
  • “funeral” is an anagram of “real fun”

Program to check if two strings are anagram

To check whether two strings are anagrams of each other in Java, you can follow these steps:

  1. Check if the two strings have the same length. If not, they cannot be anagrams.
  2. Create two arrays of the same size as the strings, and initialize them to zero.
  3. Iterate through the first string, and for each character, increment the corresponding element in the first array.
  4. Iterate through the second string, and for each character, increment the corresponding element in the second array.
  5. Check if the two arrays are equal. If they are, the two strings are anagrams. If they are not, the two strings are not anagrams.
Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        String s1 = "listen";
        String s2 = "silent";

        if (areAnagrams(s1, s2)) {
            
            // prints true if the strings are anagrams
            System.out.println("The strings are anagrams.");
        } else {
            
            // prints false if the strings are not anagrams
            System.out.println("The strings are not anagrams.");
        }
    }

public static boolean areAnagrams(String s1, String s2){
    
        // Check if the two strings have the same length
        if (s1.length() != s2.length()) {
            return false;
        }
        // Create two arrays to store the character counts
        int[] count1 = new int[128];
        int[] count2 = new int[128];

        // Iterate through the first string and increment the
        // corresponding element in the first array
        for (int i = 0; i < s1.length(); i++) {
            count1[s1.charAt(i)]++;
        }

        // Iterate through the second string and increment the
        // corresponding element in the second array
        for (int i = 0; i < s2.length(); i++) {
            count2[s2.charAt(i)]++;
        }

        // Check if the two arrays are equal
        for (int i = 0; i < 128; i++) {
            if (count1[i] != count2[i]) {
                return false;
            }
        }
        return true;
    }
}

Output:

The strings are anagrams.

This program will print “The strings are anagrams.” if the strings s1 and s2 are anagrams, and “The strings are not anagrams.” if they are not.
You can test the program with different strings by changing the values of s1 and s2.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription