How to check if two strings are anagrams of each other in java?

We can say if two strings are an anagram of each other if they contain the same characters but at different orders. For Ex. army & mary

Solution

step1: convert both strings into a character array by making strings in lowercase.

step2: Sort both character arrays.

step3: Compare both character arrays to check whether both arrays are equals or not. if equals then both strings are an anagram.

Let’s see the complete logic below:

CheckAnagramString.java

package com.javacodepoint.programs;

import java.util.Arrays;

public class CheckAnagramString {

	//Main method
	public static void main(String[] args) {
		
		//initializing two string to check anagram string
		String str1 = "Army";
		String str2 = "mary";

		System.out.println("Checking whether str1 and str2 are an anagram of each other or not: "
				+ checkAnagram(str1, str2));
	}

	//Method to check anagram string
	public static boolean checkAnagram(String s1, String s2) {
		
		//converting into char array from string
		char[] s1CharArray = s1.toLowerCase().toCharArray();
		char[] s2CharArray = s2.toLowerCase().toCharArray();
		
		//sorting the char array
		Arrays.sort(s1CharArray);
		Arrays.sort(s2CharArray);

		//checking whether both char array equals or not
		//if equals then both strings are anagram of each other
		return Arrays.equals(s1CharArray, s2CharArray);

	}

}

OUTPUT:

Checking whether str1 and str2 are an anagram of each other or not: true

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *