Practice Problem

Isomorphic Strings

Difficulty: Easy

Determine if two strings are isomorphic, where each character in one string can be mapped to exactly one character in the other.

Isomorphic Strings

Given two strings s and t, determine if they are isomorphic.

Two strings are isomorphic if the characters in s can be replaced to get t, with the following rules:

  • Each character in s maps to exactly one character in t.
  • No two characters in s map to the same character in t.
  • The order of characters is preserved.

Examples

Example 1:

Input: s = "egg", t = "add"
Output: true
Explanation: 'e' -> 'a', 'g' -> 'd'. The mapping is consistent and one-to-one.

Example 2:

Input: s = "foo", t = "bar"
Output: false
Explanation: 'f' -> 'b', 'o' -> 'a', but then the second 'o' should map to 'a', not 'r'. Wait, actually 'o' maps to 'a' first time, but 'o' at index 2 would need to map to 'r'. Contradiction.

Example 3:

Input: s = "paper", t = "title"
Output: true
Explanation: 'p' -> 't', 'a' -> 'i', 'e' -> 'l', 'r' -> 'e'. Consistent one-to-one mapping.

Constraints

  • 1 <= s.length <= 5 * 10^4
  • s.length == t.length
  • s and t consist of any valid ASCII characters.

Expected Complexity

  • Time: O(n)
  • Space: O(1). at most 128 ASCII character mappings
EASY
Arrays
Hash Map / Dictionary
Strings
Beginner

0 views

Solution

Hints

Hint 1
Hint 2
Premium
Hint 3
Premium