Monday, May 2, 2022

Question 2 : Write a java program to check if two Strings are anagram in java?

Solution: Two strings are anagrams if they have the same characters but in different order. For example, Angel and Angle are anagrams

There are a few ways to check if Strings are anagrams. Some of them are:

  • Using String methods
  • Using array.sort
  • Using count array
  • Using Java 8

Using String methods

Algorithm:
  1. Pass two Strings word and anagram to method called isAnagramUsingStringMethods()
  2. Iterate over first String word and get char c from it using charAt() method
  3. If index of char c is -1 in second String anagram, then two strings are not anagrams
  4. If index of char c is not equal to -1 in second String anagram, then remove the character from the String anagram.
  5. If you get empty String in the end, then two Strings are anagrams of each other.
package org.cloudTechtwitter; public class StringAnagramMain { public static void main(String[] args) { String word = "cloudTechtwitter"; String anagram = "TechtwitterCloud"; System.out.println("cloudTechtwitter and TechtwitterCloud are anagrams :" + isAnagramUsingStringMethods(word, anagram)); } public static boolean isAnagramUsingStringMethods(String word, String anagram) { if (word.length() != anagram.length()) return false; for (int i = 0; i < word.length(); i++) { char c = word.charAt(i); int index = anagram.indexOf(c); // If index of any character is -1, then two strings are not anagrams // If index of character is not equal to -1, then remove the chacter from the // String if (index != -1) { anagram = anagram.substring(0, index) + anagram.substring(index + 1, anagram.length()); } else return false; } return anagram.isEmpty(); } }
When you run above program, you will get below output:
CloudTechtwitter and TechtwitterCloud are anagrams :true

Using Arrays.sort()

You can simply sort both the Strings using Arrays.sort() method. If both the Strings are equal after Sorting, then these two Strings are anagram of each other.:

package org.TechtwitterCloud; import java.util.Arrays; public class AnagramUsingSort { public static void main(String[] args) { String word = "CloudTechtwitter"; String anagram = "TechtwitterCloud"; System.out.println("TechtwitterCloud and CloudTechtwitter are anagrams :" + isAnagramUsingArraySort(word, anagram)); } public static boolean isAnagramUsingArraySort(String word, String anagram) { String sortedWord = sortChars(word); String sortedAnagram = sortChars(anagram); return sortedWord.equals(sortedAnagram); } public static String sortChars(String word) { char[] wordArr = word.toLowerCase().toCharArray(); Arrays.sort(wordArr); return String.valueOf(wordArr); } }

When you run above program, you will get below output:

TechtwitterCloud and CloudTechtwitter are anagrams :true

Using Java 8

public static Boolean isAnagram(String word1, String word2){ List<String> listWord1 = new ArrayList<>(Arrays.asList(word1.split(""))); List<String> listWord2 = new ArrayList<>(Arrays.asList(word2.split(""))); Collections.sort(listWord1); Collections.sort(listWord2); word1 = String.join("", listWord1); word2 = String.join("", listWord2); return word1.equals(word2); }

Using count array

Here is another approach to find if two Strings are anagrams.

  1. Pass two Strings str1 and str2 to method isAnagram()
  2. If length of str1 and str2 are not same, then they are not anagrams
  3. Create an array named count of 256 length
  4. Iterate over first string str1
  5. In each iteration, we increment count of first String str1 and decrement the count of second String str2
  6. If count of any character is not 0 at the end, it means two Strings are not anagrams

This approach has time complexity of O(n), but it requires extra space for count Array.:

package org.TechtwitterCloud; public class AnagramCountingMain { public static void main(String args[]) { boolean isAnagram = isAnagram("Angle","Angle"); System.out.println("Are Angle and Angel anangrams: "+isAnagram); } public static boolean isAnagram(String str1, String str2) { if (str1.length() != str2.length()) { return false; } int count[] = new int[256]; for (int i = 0; i < str1.length(); i++) { count[str1.charAt(i)]++; count[str2.charAt(i)]--; } for (int i = 0; i < 256; i++) { if (count[i] != 0) { return false; } } return true; } }

You may also like

Kubernetes Microservices
Python AI/ML
Spring Framework Spring Boot
Core Java Java Coding Question
Maven AWS