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:- Pass two Strings
word
and anagram
to method called isAnagramUsingStringMethods()
- Iterate over first String
word
and get char c
from it using charAt()
method - If index of char c is
-1
in second String anagram
, then two strings are not anagrams - If index of char c is not equal to -1 in second String
anagram
, then remove the character from the String anagram
. - 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
Algorithm:
- Pass two Strings
word
andanagram
to method calledisAnagramUsingStringMethods()
- Iterate over first String
word
and get charc
from it usingcharAt()
method - If index of char c is
-1
in second Stringanagram
, then two strings are not anagrams - If index of char c is not equal to -1 in second String
anagram
, then remove the character from the Stringanagram
. - 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
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.
- Pass two Strings str1 and str2 to method
isAnagram()
- If length of str1 and str2 are not same, then they are not anagrams
- Create an array named
count
of 256 length - Iterate over first string
str1
- In each iteration, we increment count of first String
str1
and decrement the count of second Stringstr2
- 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; } }