Data Structures & AlgorithmsModerateProduct placement · Amazon/Microsoft

How many characters must be deleted in total from two strings so that they become anagrams of each other? What is printed?

How many characters must be deleted in total from two strings so that they become anagrams of each other? What is printed?
from collections import Counter
a, b = "dcbebaec", "ebaeab"
ca, cb = Counter(a), Counter(b)
print(sum(((ca - cb) + (cb - ca)).values()))
  1. A. 5
  2. B. 4
  3. C. 3
  4. D. 6

Correct answer: B. 4

Each letter contributes |count in a − count in b| deletions (Counter subtraction drops non-positive counts, so both directions are needed). Total: 4.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic