← Questions

Equalize two strings' letter counts by deleting one letter

0
AmazonCodingLast asked Sep 2025

A coding interview question asked in Amazon's online assessment, last asked Sep 2025. It tests strings and counting.

You get two lowercase strings s and t. In s you may pick one letter and delete any number of its occurrences (including none); independently, in t you may pick one letter, possibly a different one, and do the same. Decide whether the two strings can end up as anagrams of each other, with every letter appearing the same number of times in both.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • can_be_similar(s: str, t: str) -> bool

Example

can_be_similar("safddadfs", "famafmss")  ->  True    # delete the d's in s and the m's in t:
                                                    # both become a×2, f×2, s×2
can_be_similar("abcc", "abd")            ->  True    # delete both c's in s, the d in t
can_be_similar("aab", "abb")             ->  True    # delete one a in s, one b in t
can_be_similar("aabbcc", "abc")          ->  False   # s has too many of three letters
can_be_similar("ab", "cd")               ->  False

Constraints

  • Lowercase English letters; either string may be empty; up to 10^5 characters each.

Clarifying questions

  • Must you delete all occurrences of the chosen letter, or may you delete only some? (Any number, including some.)
  • May the letters chosen in the two strings differ? (Yes.)
  • Is doing nothing allowed? (Yes.)

What a strong answer covers

  • Count the letters of both strings and compare: deleting can only lower one letter's count in each string. A letter where s has more must be the one chosen in s, and a letter where t has more must be the one chosen in t.
  • So the answer is True exactly when at most one letter has a higher count in s and at most one letter has a higher count in t. O(n + 26) time, O(26) space.
  • A brute-force check over all 26 × 26 letter pairs as a cross-check.
  • Edge cases: empty strings, identical strings, a letter present in only one string.

Follow-ups

  • You must delete all occurrences of the chosen letter, not just some. What changes?
  • You may choose up to k letters in each string.

Where it's asked

  • Amazon · Online assessment · Last asked Sep 2025Coding problems plus a repository bug-fix section with a built-in assistant or a design scenario, then a work-style survey · AI only inside the bug-fix section's built-in assistant

What it tests

strings · counting

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding