Data Structures & AlgorithmsModerateGATE CS · DSA
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 = "ddbabbeb", "cddbee" ca, cb = Counter(a), Counter(b) print(sum(((ca - cb) + (cb - ca)).values()))
- A. 6
- B. 4
- C. 2
- D. 14
Correct answer: A. 6
Each letter contributes |count in a − count in b| deletions; Counter subtraction drops non-positive counts, so both directions are needed: 4 + 2 = 6. Using only ca − cb gives 4, the length difference is 2 and 14 is the total length.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A hash-map two-sum returns the first index pair (i, j), i < j, found while scanning left to right. What is printed? python def twosum(a…
- Evaluate an infix expression of single digits with two stacks (operands and operators, usual precedence, left-associative). What is…
- Reverse a singly linked list in groups of k (a final group shorter than k is left as it is). What is printed? python def revk(h, k): cnt…
- Integer square root by binary search (largest m with m·m ≤ n). What is printed (root, iterations)? python n, lo, hi, it = 777, 0, 777, 0…
- Merge two sorted linked lists by relinking nodes (ties take the node from the first list). What is printed? python def merge(a, b): d = t…
- Binary search with mid = (lo + hi) // 2 (lo = 0, hi = n − 1, loop while lo ≤ hi) looks for 81 in [22, 26, 29, 31, 36, 38, 46, 51, 70, 81…