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()))
- A. 5
- B. 4
- C. 3
- 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.
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
- What does this loop-counting code print? (Python) python n, c = 33, 0 i = 1 while i < n: j = i while j < n: c += 1 j = 2 i += 1 print(c)
- Evaluate an infix expression of single digits with two stacks (operands and operators, usual precedence, left-associative). What is…
- One Lomuto partition (pivot = LAST element; i marks the end of the 'less than pivot' region; when a[j] < pivot, increment i and swap a[i]…
- Insertion sort (ascending; shift larger elements right, one key comparison per inner-loop test, the loop stops at the first element ≤ the…
- One Hoare partition with the FIRST element as pivot (i moves right while a[i] < pivot, j moves left while a[j] > pivot, swap while i < j…
- How many inversions (pairs i < j with a[i] > a[j]) does [5, 6, 10, 37, 38, 4, 17, 23] have? (This is also the number of swaps bubble sort…