Dataford
Interview Guides
Upgrade
All questions/Coding/Check Whether Two Strings Are Anagrams

Check Whether Two Strings Are Anagrams

Easy
Coding
Asked at 1 company1StringsHash TablesSorting
Also asked at
Walmart

Problem

Given two strings s and t, return True if they are anagrams and False otherwise. Two strings are anagrams if they contain the same characters with the same frequencies.

Examples

Example 1
Inputs = "anagram", t = "nagaram"OutputTrueWhyThe characters and their counts match exactly.
Example 2
Inputs = "rat", t = "car"OutputFalseWhyThe strings contain different letters.

Constraints

  • `1 <= len(s), len(t) <= 10^5`
  • `s` and `t` consist of lowercase English letters
  • A frequency-based solution is expected

Problem

Given two strings s and t, return True if they are anagrams and False otherwise. Two strings are anagrams if they contain the same characters with the same frequencies.

Examples

Example 1
Inputs = "anagram", t = "nagaram"OutputTrueWhyThe characters and their counts match exactly.
Example 2
Inputs = "rat", t = "car"OutputFalseWhyThe strings contain different letters.

Constraints

  • `1 <= len(s), len(t) <= 10^5`
  • `s` and `t` consist of lowercase English letters
  • A frequency-based solution is expected
Practice Python
Python 3.10
Open on desktop for the full Python editor with syntax highlighting and autocomplete.