Member-only story
Interview question: Calculate how to prove two strings are anagrams
An anagram is defined as a word, phrase or name that is created by rearranging letters of another. Computer technology has made anagrams relatively easy to pick out, and it is for that reason that a person can be asked how to prove whether a string is or is not an anagram during a coding interview.
The pseudocode to find out if two strings are anagrams is:-
- Define the two strings that you want to determine whether they are anagrams, as the variables s1 and s2.
- Convert the two variables to lower case.
- Define a function, anagram, which will take in the two variables previously defined. If sorted s1 is the same as sorted s2 then the two strings are anagrams, otherwise they are not.
The Python code below is an example of how to determine if two strings are anagrams:-
Because the algorithm to determine whether two strings are anagrams is rather straightforward, it is likely to be asked at a coding interview. It is important, therefore, to become familiar with this solution to enhance chances of success at an interview.