How to check whether two strings are anagram or not?

Priti Jha
2 min readDec 29, 2022

An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. For example, “silent” is an anagram of “listen” and “rest” is an anagram of “ster”.

To determine if a given string is an anagram, you can follow these steps:

  1. Convert both strings to lowercase or uppercase, so that the case of the letters does not affect the comparison.
  2. Sort the letters of both strings alphabetically. For example, the string “silent” would become “eilnst” and the string “listen” would become “eilnst”.
  3. Compare the sorted strings. If they are the same, then the original strings are anagrams. If they are different, then the original strings are not anagrams.

You can also use the following method to determine if a given string is an anagram:

  1. Convert both strings to lowercase or uppercase, so that the case of the letters does not affect the comparison.
  2. Count the number of occurrences of each letter in both strings. You can do this by creating a dictionary or hash map, where the keys are the letters and the values are the number of occurrences.
  3. Compare the dictionaries or hash maps. If they are the same, then the original strings are anagrams. If they are different, then the original strings are not anagrams.

--

--

Priti Jha
Priti Jha

Written by Priti Jha

Senior front-end developer writing about Angular 8 , Ionic Framework ,Javascript, HTML, Css and all front end technology.

No responses yet