FastPrepCheck Anagrams Without Sorting
Problem · String

Check Anagrams Without Sorting

Learn this problem
EasyAmazon logoAmazonNEW GRADONSITE INTERVIEW
See Amazon hiring insights

Problem statement

Given two lowercase English strings first and second, return whether they are anagrams.

Two strings are anagrams when every letter has the same frequency in both strings. Solve the task with character-frequency counting; do not sort either string.

Function

areAnagrams(first: String, second: String) → boolean

Examples

Example 1

first = "listen"second = "silent"return = true

Both words contain the same six letters with equal frequencies.

Example 2

first = "aab"second = "abb"return = false

The multiplicities of a and b differ.

Example 3

first = "amazon"second = "amazon"return = true

A string is an anagram of itself.

Constraints

  • 1 <= first.length, second.length <= 100000.
  • Both strings contain only lowercase English letters.
  • Do not sort the strings.

More Amazon problems

drafts saved locally
public boolean areAnagrams(String first, String second) {
  // write your code here
}
first"listen"
second"silent"
expectedtrue
checking account