This post is all about write a java program to check if two strings are anagrams or not in java
Write a Java program to find whether the given strings are anagrams or not is a very frequently asked interview question these days no matter how much experience do you have.
What is an anagram?
We can say that two strings are called anagrams if we can rearrange the letters of one string to produce the second string, using all the letters of the first string only once. While doing that, usually, you don’t consider spaces and punctuation marks.
Few examples Like-“Listen”and “Silent”,forty five” and “over fifty”, “restful” and “fluster”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
package com.kkjavatutorials.client; import java.util.Arrays; /** * @author KK JavaTutorials * This Java program is all about how to checks whether the given input strings are anagrams or not */ public class AnagramTest { public static void main(String[] args) { //Check Anagram Using Sorting logic isAnagramsBySortingLogic("restful", "fluster"); isAnagramsBySortingLogic("restful", "fluser"); isAnagramsBySortingLogic("forty five", "over fifty"); System.out.println("-----------------------------------------------------"); //Check Anagram using iteration and deletion logic isAnagramsByIterationLogic("xyz", "yz X"); isAnagramsByIterationLogic("xyz", "yx X"); isAnagramsByIterationLogic("rail safety", "Fairy tales"); } /** * This method has logic to find out if two input strings are anagrams or not using sorting logic * @param inputText1 * @param inputText2 */ public static void isAnagramsBySortingLogic(String inputText1, String inputText2) { //convert String to lower case and remove spaces String s1 = convertInputInLowerCaseAndRemoveSpaces(inputText1); String s2 = convertInputInLowerCaseAndRemoveSpaces(inputText2); boolean flag = true; // Here we are checking String length if both strings have different length then it is not anagrams if (s1.length() != s2.length()) { flag = false; } //If we find both Strings have same length then sort both the strings s1 = sortInputString(s1); s2 = sortInputString(s2); flag = s1.equals(s2); isStringAnagram(inputText1, inputText2, flag); } /** * This method has logic to find out if two input strings are anagrams or not using iteration and deletion logic * @param inputText1 * @param inputText2 */ public static void isAnagramsByIterationLogic(String inputText1, String inputText2) { //convert String to all lower case and remove spaces String s1 = convertInputInLowerCaseAndRemoveSpaces(inputText1); String s2 = convertInputInLowerCaseAndRemoveSpaces(inputText2); boolean flag = true; int index; // check for length if (s1.length() != s2.length()) { flag = false; } char[] strArray = s1.toCharArray(); StringBuilder sb = new StringBuilder(s2); for (char c : strArray) { // Look for the char in second String index = sb.indexOf("" + c); /*If we find char delete it,otherwise second occurrence of the same char in first input string will be found in second input String*/ if (index != -1) { sb.deleteCharAt(index); } else { flag = false; break; } } isStringAnagram(inputText1, inputText2, flag); } //Sort the given input String private static String sortInputString(String str) { char[] charArray = str.toCharArray(); Arrays.sort(charArray); return new String(charArray); } // This method basically converts String to all lower cases and remove spaces protected static String convertInputInLowerCaseAndRemoveSpaces(String str) { return str.toLowerCase().replaceAll("\\s", ""); } private static void isStringAnagram(String inputText1, String inputText2, boolean flag) { if (flag) { System.out.println(inputText1 + " and " + inputText2 + " are anagrams"); } else { System.out.println(inputText1 + " and " + inputText2 + " are not anagrams"); } } } |
If you run above program in your favourite IDE then you will get blow output:
1 2 3 4 5 6 7 |
restful and fluster are anagrams restful and fluser are not anagrams forty five and over fifty are anagrams ----------------------------------------------------- xyz and yz X are anagrams xyz and yx X are not anagrams rail safety and Fairy tales are anagrams |
You would love to watch me on YouTube:
That’s all about this post How to check if two strings are anagrams or not in java.
You May Also Like:
Reverse a string in Java (6 Different Ways)
Splitting a String in Java using a delimiter
Count number of words in a string java
Count total number of times each character appears in the string in java
How to convert string to int without using library functions in java
Check Whether a Given String/Number is a Palindrome in java
How to find first non-repeated character in a given String in Java
How to find first non-repeatable character from a String using Java 8
Java Program to find the frequency of each character in String ?
If you have any feedback or suggestion please feel free to drop in below comment box.