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))
  1. A. 1 3
  2. B. 1 5
  3. C. 3 1
  4. 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.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic