Data Structures & AlgorithmsVery hardGATE CS · TCS NQT

Which method checks in O(n) whether two strings are isomorphic (a one-to-one character mapping turns one into the other)?

  1. A. Sorting both strings and comparing
  2. B. Two arrays or hash maps tracking the mapping in both directions
  3. C. Comparing all pairs of characters
  4. D. Computing the longest common subsequence

Correct answer: B. Two arrays or hash maps tracking the mapping in both directions

Maintaining a mapping in both directions detects a clash in one pass; sorting ignores positions and so cannot test a consistent mapping.

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

Exams that ask this topic