Data Structures & AlgorithmsModerateGATE CS · DSA
Group words that are anagrams of each other using the sorted word as the key. What is printed?
Group words that are anagrams of each other using the sorted word as the key. What is printed?
from collections import defaultdict
g = defaultdict(list)
for w in ['dog', 'act', 'enlist', 'gogole', 'tinsel', 'odg', 'cat', 'silent']:
g["".join(sorted(w))].append(w)
print(len(g), max(len(v) for v in g.values()))- A. 2 4
- B. 4 5
- C. 6 3
- D. 4 3
Correct answer: D. 4 3
Anagrams share the same sorted letters, so they land in one bucket. Output (groups, largest group): 4 3.
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
- Product of all elements except self, using prefix and suffix products (no division). What is printed? python a = [-1, 0, 2, 2, 2] n =…
- What is the maximum product of a non-empty contiguous subarray of [−2, 4, 3, −1, −2, −3, −2, 0]? (Track both the maximum and the minimum…
- Group words that are anagrams of each other using the sorted word as the key. What is printed? python from collections import defaultdict…
- What does this loop-counting code print? (Python) python n, c = 37, 0 i = n while i > 0: for j in range(i): c += 1 i //= 2 print(c)
- Top-down merge sort (split at mid = n // 2, recurse, then merge, stopping comparisons when one side runs out) sorts [14, 8, 20, 57, 38, 4…
- Longest substring that can be turned into one repeated letter with at most k replacements (sliding window, window − maxfreq ≤ k). What is…