Java program to check for Anagram

An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “abcd” and “dabc” are an anagram of each other.

We are given two strings, we need to check if strings are Anagram.

Examples:

Input : "anagram", "nagaram"
Output : yes

Input : "mat", "fat"
Output : no


Method 1: Check if Two Strings Are Anagram using Array

import java.util.Arrays;

public class Main {

public static boolean checkAnagram(String s1, String s2)
{
// Remove all the white space
s1 = s1.replaceAll("\\s", "");
s2 = s2.replaceAll("\\s", "");

// Check if both length matches
if(s1.length() != s2.length())
return false;
else
{
// Convert both Strings into lower case and into Character Array
char[] arr1 = s1.toLowerCase().toCharArray();
char[] arr2 = s2.toLowerCase().toCharArray();

// Sort both Character Array
Arrays.sort(arr1);
Arrays.sort(arr2);

// Check if both arrays are equal
return (Arrays.equals(arr1, arr2));
}
}
public static void main(String []args)
{
System.out.println(checkAnagram("anagram","nagaram"));
System.out.println(checkAnagram("mat","fat"));
}
}

Console Output:
true
false



Method 2: Check Anagram using HashMap

import java.util.HashMap;

public class Main {

public static boolean checkAnagram(String s1, String s2)
{
if (s1.length() != s2.length())
return false;
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for (int i = 0; i < s1.length(); i++)
{
char c = s1.charAt(i);
if (map.containsKey(c))
map.put(c, map.get(c) + 1);
else
map.put(c, 1);
}
for (int i = 0; i < s2.length(); i++)
{
char c = s2.charAt(i);
if (map.containsKey(c))
{
if (map.get(c) == 1)
map.remove(c);
else
map.put(c, map.get(c) - 1);
} else
return false;
}
if (map.size() > 0)
return false;
return true;
}

public static void main(String []args)
{
System.out.println(checkAnagram("anagram","nagaram"));
System.out.println(checkAnagram("mat","fat"));
}
}

Console Output:
true
false



Method 3: Anagram Program using XOR

public class Main {

public static boolean checkAnagram(String s1, String s2)
{
// Remove all the white space, convert
// to lower case & character array
char[] arr1 = s1.replaceAll("\\s", "")
.toLowerCase().toCharArray();
char[] arr2 = s2.replaceAll("\\s", "")
.toLowerCase().toCharArray();
if (arr1.length != arr2.length)
return false;

int xor = 0;

for (int i = 0; i < arr1.length; i++)
{
xor ^= arr1[i] ^ arr2[i];
}

return xor == 0? true: false;
}

public static void main(String []args)
{
System.out.println(checkAnagram("anagram","nagaram"));
System.out.println(checkAnagram("mat","fat"));
}
}


Console Output:
true
false

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete