Data Structures & AlgorithmsHardGATE CS · DSA
Two strings are isomorphic if characters can be replaced one-to-one to turn one into the other. Version A checks both directions; version B checks only s → t. What is printed?
Two strings are isomorphic if characters can be replaced one-to-one to turn one into the other. Version A checks both directions; version B checks only s → t. What is printed?
def iso(s, t, both=True):
m1, m2 = {}, {}
for a, b in zip(s, t):
if m1.setdefault(a, b) != b: return False
if both and m2.setdefault(b, a) != a: return False
return True
ps = [('abc', 'xyz'), ('foo', 'bar'), ('badc', 'baba'), ('paper', 'title'), ('ab', 'aa')]
print(sum(iso(s, t) for s, t in ps), sum(iso(s, t, False) for s, t in ps))- A. 4 4
- B. 2 5
- C. 2 4
- D. 4 2
Correct answer: C. 2 4
A one-way map lets two different letters map to the same letter (e.g. 'ab' → 'aa'); a bijection needs the reverse map too. Output: 2 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
- Swap every two adjacent nodes of a linked list by relinking (not by swapping values). What is printed? python def swappairs(h): d = p =…
- Count distinct values in every window of size k with a frequency map. What is printed? python from collections import Counter a, k = [3…
- A min-stack keeps a second stack of running minimums. What is printed? python st, mn, out = [], [], [] for op in [7, 5, 7, 5, 5, "pop"…
- 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…
- A dynamic array doubles its capacity when full, copying all elements. Starting from capacity 1, what is the total number of element copies…
- Evaluate the postfix expression 7 4 1 ÷ 7 + 6 × 2 × + 3 − (all divisions are exact). What is its value, and what is the maximum number of…