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)?
- A. Sorting both strings and comparing
- B. Two arrays or hash maps tracking the mapping in both directions
- C. Comparing all pairs of characters
- 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.
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
- Which technique counts the inversions of an array of n distinct integers in O(n log n) time?
- How many contiguous subarrays of [2, 4, 6, 3, 9] have an odd sum?
- What is the length of the longest proper border of the string aabaaab (the longest proper prefix that is also a suffix)?
- Which algorithm finds all occurrences of a pattern in a text in expected linear time using a rolling hash of each window?
- You must perform exactly 3 operations on the array [4, -2, 5, -7, 3, -1]; each operation replaces one element by its negation (the same…
- What is the length of the longest substring of "aaabbcccbbaad" in which EVERY letter that appears in the substring appears at least 2 times?