Data Structures & AlgorithmsModerateInfosys SP/DSE · Product Company SDE
Problem: Check whether two strings are isomorphic (characters map one-to-one consistently).
Problem: Check whether two strings are isomorphic (characters map one-to-one consistently).
def iso(s, t):
return len(set(s)) == len(set(t)) == len(set(zip(s, t)))
print(iso("egg", "add"), iso("foo", "bar"), iso("badc", "baba"), iso("paper", "title"))What is printed?- A. True False True True
- B. True False False True
- C. True True False True
- D. False False False True
Correct answer: B. True False False True
A valid bijection means the number of distinct (s, t) pairs equals the distinct letters on each side. "badc" has 4 distinct letters but "baba" only 2, so two letters of s would have to share an image (a→a and c→a). "foo" fails the other way: 'o' must map to both 'a' and 'r'. One-directional checks pass such cases by mistake. O(n).
More Coding Interview Problems questions
- Problem: Find the minimum number of swaps to sort an array of distinct values. python def minswaps(a): order = sorted(range(len(a))…
- Problem: Count the distinct values taken by the bitwise OR of all contiguous subarrays. python def distinctor(a): res, cur = set(), set()…
- Problem: Compute the maximum XOR of any two numbers using a greedy prefix-set approach bit by bit. python def maxxor(a): ans = mask = 0…
- Problem: Implement atoi: skip leading spaces, read an optional sign and digits, stop at the first non-digit, clamp to 32-bit range. python…
- Problem: Count the number of ways to climb n stairs taking 1, 2 or 3 steps, modulo 10⁹ + 7. java static long ways3(int n) { long MOD =…
- Problem: A frog jumps from stone 0 to stone n − 1; jumping from i to j (j ≤ i + k) costs |h[i] − h[j]|. Find the minimum cost. python def…