Skip to content
CodeKruPro

Check whether two strings are anagrams

Coding problemEasyAsked in: Capgemini

Two strings are anagrams when one uses exactly the same characters as the other, the same number of times, in any order. Given two strings a and b, return true if they are anagrams, and false otherwise.

Ignore spaces and ignore upper and lower case: "Dormitory" and "dirty room" are anagrams. Two strings with no characters other than spaces are anagrams of each other.

Examples

Example 1
Input: a = "listen", b = "silent"
Output: true
Example 2
Input: a = "Dormitory", b = "dirty room"
Output: true
After dropping the space and making both lower case, both use the same letters.
Example 3
Input: a = "aab", b = "abb"
Output: false
Same letters, different counts.

Constraints

  • The length of a and of b is between 0 and 100,000
  • Both hold printable ASCII characters only

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in