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 = [('badc', 'baba'), ('aab', 'xyy'), ('ab', 'aa'), ('egg', 'add'), ('foo', 'bar')]
print(sum(iso(s, t) for s, t in ps), sum(iso(s, t, False) for s, t in ps))- A. 1 3
- B. 1 5
- C. 3 1
- D. 2 3
Correct answer: A. 1 3
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: 1 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
- Trains occupy a platform from arrival to departure INCLUSIVE, so a train arriving at the same minute another departs needs a separate…
- Which statement about the KMP failure (lps) array of a pattern of length m is correct?
- Find the leaders (elements strictly greater than everything to their right) by scanning from the right. What is printed? python a = [18…
- Integer square root by binary search (largest m with m·m ≤ n). What is printed (root, iterations)? python n, lo, hi, it = 200, 0, 200, 0…
- Items E, F, G, H, I, J are pushed onto an empty stack in this order, and a pop may happen at any time. Which pop order is NOT possible?
- Exponential search: double the bound i (1, 2, 4, …) until a[i] ≥ key or i passes the end, then binary-search the last range. How many…