Strings are Anagram in Java

What is Anagram ?

Strings are called anagram, if they contains the same characters but on different order e.g. army and mary, stop and pots, etc.

Example – Program to check whether two strings are anagram or not?

public class AnagramCheck {

  /*
   * assumes both arguments are not null 
   * @return true, if both String are anagram
   */
  public static boolean isAnagram(String word, String anagram) {
    word = word.toLowerCase();
    anagram = anagram.toLowerCase();
    if (word.length() != anagram.length()) {
      return false;
    }

    char[] chars = word.toCharArray();

    for (char c : chars) {
      int index = anagram.indexOf(c);
      if (index != -1) {
        anagram = anagram.substring(0, index) + anagram.substring(index + 1, anagram.length());
      } else {
        return false;
      }
    }

    return anagram.isEmpty();
  }

  
  public static void main(String[] args) {
    String str1 = "word";
    String str2 = "wrdo";

    if (isAnagram(str1, str2)) {
      System.out.println("'" + str1 + "' and '" + str2 + "' is anagram");
    } else {
      System.out.println("'" + str1 + "' and '" + str2 + "' is not anagram");

    }

    // //////////////
    String str3 = "SiLeNt CAT";
    String str4 = "LisTen AcT";

    if (isAnagram(str3, str4)) {
      System.out.println("'" + str3 + "' and '" + str4 + "' is anagram");
    } else {
      System.out.println("'" + str3 + "' and '" + str4 + "' is not anagram");

    }
    // ////////////

    String str5 = "Debit Card";
    String str6 = "Bad Credit";

    if (isAnagram(str5, str6)) {
      System.out.println("'" + str5 + "' and '" + str6 + "' is anagram");
    } else {
      System.out.println("'" + str5 + "' and '" + str6 + "' is not anagram");

    }

  }
}

Output

'word' and 'wrdo' is anagram
'SiLeNt CAT' and 'LisTen AcT' is anagram
'Debit Card' and 'Bad Credit' is anagram

Example -Using Arrays.sort() method

import java.util.Arrays;

public class AnagramCheck {
  
  /*
   * Another way to check if two Strings are anagram or not in Java This
   * method assumes that both word and anagram are not null 
   * @return true, if both Strings are anagram.
   * sorting is done a/c to asc alphabetical order
   */
  public static boolean isAnagram(String str1, String str2) {
    str1 = str1.toLowerCase();
    str2 = str2.toLowerCase();
    char[] charFromstr1 = str1.toCharArray();
    char[] charFromstr2 = str2.toCharArray();
    Arrays.sort(charFromstr1);
    Arrays.sort(charFromstr2);

    return Arrays.equals(charFromstr1, charFromstr2);
  }

  public static void main(String[] args) {
    String str1 = "word";
    String str2 = "wrdo";

    if (isAnagram(str1, str2)) {
      System.out.println("'" + str1 + "' and '" + str2 + "' is anagram");
    } else {
      System.out.println("'" + str1 + "' and '" + str2 + "' is not anagram");

    }

    // //////////////
    String str3 = "SiLeNt CAT";
    String str4 = "LisTen AcT";

    if (isAnagram(str3, str4)) {
      System.out.println("'" + str3 + "' and '" + str4 + "' is anagram");
    } else {
      System.out.println("'" + str3 + "' and '" + str4 + "' is not anagram");

    }
    // ////////////

    String str5 = "Debit Card";
    String str6 = "Bad Credit";

    if (isAnagram(str5, str6)) {
      System.out.println("'" + str5 + "' and '" + str6 + "' is anagram");
    } else {
      System.out.println("'" + str5 + "' and '" + str6 + "' is not anagram");

    }

  }
}

Output

'word' and 'wrdo' is anagram
'SiLeNt CAT' and 'LisTen AcT' is anagram
'Debit Card' and 'Bad Credit' is anagram

Example – Using Another way

public class AnagramCheck {

  public static boolean isAnagram(String str1, String str2) {
    
    str1=str1.toLowerCase();
    str2=str2.toLowerCase();

    char[] char1 = str1.toCharArray();
    StringBuilder sbStr2 = new StringBuilder(str2);

    for (char ch : char1) {
      int index = sbStr2.indexOf("" + ch);
      if (index != -1) {
        sbStr2.deleteCharAt(index);
      } else {
        return false;
      }
    }

    return sbStr2.length() == 0 ? true : false;
  }

  public static void main(String[] args) {
    String str1 = "word";
    String str2 = "wrdo";

    if (isAnagram(str1, str2)) {
      System.out.println("'" + str1 + "' and '" + str2 + "' is anagram");
    } else {
      System.out.println("'" + str1 + "' and '" + str2 + "' is not anagram");

    }

    // //////////////
    String str3 = "SiLeNt CAT";
    String str4 = "LisTen AcT";

    if (isAnagram(str3, str4)) {
      System.out.println("'" + str3 + "' and '" + str4 + "' is anagram");
    } else {
      System.out.println("'" + str3 + "' and '" + str4 + "' is not anagram");

    }
    // ////////////

    String str5 = "Debit Card";
    String str6 = "Bad Credi";

    if (isAnagram(str5, str6)) {
      System.out.println("'" + str5 + "' and '" + str6 + "' is anagram");
    } else {
      System.out.println("'" + str5 + "' and '" + str6 + "' is not anagram");

    }

  }
}

Output

'word' and 'wrdo' is anagram
'SiLeNt CAT' and 'LisTen AcT' is anagram
'Debit Card' and 'Bad Credi' is not anagram