Latest :

Java : Check if Two Strings Are Anagrams 2 Ways | Java Programs

This Java program is to check if two strings are anagrams. In this particular java anagram program to check two strings are anagram to each other or not, we will add suitable examples & sample output as well.

This code is for finding an anagram of a string in another string using Java language.

  • The problem here is to find and check two strings are anagram to each other, a user-defined string within another string.
  • The input here is two user-defined strings.
  • The output here is a boolean value of either true or false signifying whether an anagram of the given word is present in the other word.

Output – 1:

  • Solution :
  1. In the first line, a new Scanner class object is initialized and a reference variable sc is set to represent the object.
  2. This class is used to take user input in Java. Scanner class is part of the java.util package and hence the import statement, in the beginning, is stated to import the functionality of the specified class.
  3. The next line prints a statement to the console instructing the user to enter a string.
  4. The .nextLine() method is called by the Scanner object sc. This method reads a line of user input of a string value and stores it to the variable s1.
  5. This same process is repeated again to store the second string in variable s2.
  6. Within, the print statement of the last line, the method compare is called and s1 and s2 are passed in as arguments.
  7. In this method, int variables n1 and n2 are declared and variable is initialized to 0. Also, a boolean variable res is initialized to the value false.
  8. n1 is used to store the length of the parameter s1 which represents the first input string and n2 is used to store the length of the second string.
  9. A new char array represented by variable ch is initialized having length n1. 
  10. This array will be used to store parts of the string s2 having length equal to n1 to check whether that particular part is an anagram of s1.
  11. If the length of s2 is less than the length of s1, it is not possible for s2 to contain an anagram of s1. An if conditional statement checks for the same and executes the code within its block is n2 greater than or equal to n1. 
  12. The method compare returns a boolean value of false otherwise signifying no anagram was found
  13. We need to run n2-n1+1 iterations to check all possible permutations of s2 to find an anagram of s1 within it.
  14. Example : If s1 has length 3 and s2 has length 5, we will need to run 5-3+1 = 3 iterations to check for the presence of an anagram. If the string s1 = car and s2 = races, we need to check ‘rac’ , ‘ace’ , ‘ces’ for the presence of an anagram. Thus, we need 3 iterations.
  15. As the for loop iterates, is set to 0 and the method STOC is called and string s2, int variable i from the loop declaration representing the iteration number of the loop (technically i+1 represents the number of iterations run), length of string s1 represented by n1 and array ch are passed in as arguments.
  16. In this method, a for loop iterates n1 number of times and stores characters at j+i position from parameter s1 which actually represents string s2 to the array c.
  17. Example : If ‘races’ is passed in as the string s2 and string s1 is ‘car’, the loop will iterates thrice since n1 =3 here. The loop in the method compare will run 3 times and the value of i will be passed as 0,1 and 2.
  18. Let us assume that the value of i is 0 for this iteration. For the first iteration of the loop in the method STOC, the first character added to c will be r (since s1.charAt(0+0) = r in races).
  19. As the loop iterates r, a, c will be stored in the array. If i would have been equal to 1, a, c, e from ‘race’ would have been stored in the array.
  • For each of the n2-n1+1 iterations, the method STOC stores different parts of string s2 in the ch array.
  • After the STOC method is implemented, two for loops run simultaneously such that for each iteration of the first loop, the second loop completes all iterations.
  • For each iteration of the outer loop, the character at the index position in s1 is compared with each element in the array ch storing a particular part of n1 length of string s2. 
  • If this condition is satisfied, that array element is changed to null or ‘\0’ value to avoid being matched again to another similar character in string s1 and int c is incremented. For each match, the implementation breaks out of the inner loop and goes to the next iteration of the outer loop.
  • For an iteration of the outer loop where all the characters in s1 match all the characters in the array, the length of c will be the same as the length of s1 which is n1 and we can also say that in such a case, an anagram has been found. In such a case, the value of res is set to true.
  • If an anagram was found, the value of res is returned as true and printed out. Otherwise a boolean value of false is returned and printed out by the print statement in the main method.
  • Here, s1 = car and s2 = race and so n1 = 3 and n2 = 4. The loop in the compare method runs 4-3+1 = 2 times.
  • For the first implementation of the STOC method, the array stores r, a, c and all these characters match with the characters in car.
  • Thus, the value of res is set to true and an anagram was successfully found, finishing the objective to check if two strings are anagrams.

Output -2 :

  • Here, s1 = bag and s2 = grab and so n1 = 3 and n2 = 4. The loop in the compare method runs 4-3+1 = 2 times.
  • No implementation of the method STOC returns an array that contains an anagram of string s1 in this case.
  • Thus, the value of res remains false and no anagram is found.

Check Also

Rhombus Star Pattern Program In Java – Patterns

Java program to print Rhombus star pattern program. We have written the below print/draw Rhombus ...