Given two strings s
and t
, determine if they are isomorphic.
Two strings s
and t
are isomorphic if the characters in s
can be replaced to get t
.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
Example 1:
Input: s = "egg", t = "add"
Output: true
Explanation:
The strings s
and t
can be made identical by:
'e'
to 'a'
.'g'
to 'd'
.Example 2:
Input: s = "foo", t = "bar"
Output: false
Explanation:
The strings s
and t
can not be made identical as 'o'
needs to be mapped to both 'a'
and 'r'
.
Example 3:
Input: s = "paper", t = "title"
Output: true
Constraints:
1 <= s.length <= 5 * 104
t.length == s.length
s
and t
consist of any valid ascii character.The problem asks to determine if two strings, s
and t
, are isomorphic. Two strings are isomorphic if there's a one-to-one mapping between their characters such that replacing each character in s
with its corresponding character in t
results in an identical string. Crucially, this mapping must be consistent; no character in s
can map to multiple characters in t
.
The most efficient approach uses two hash maps (or arrays, for better performance if the character set is known and limited, as in this case - ASCII characters). One map (d1
) tracks the mapping from characters in s
to characters in t
, and the other (d2
) tracks the reverse mapping.
Algorithm:
Initialization: Create two hash maps (or arrays of size 256 for ASCII characters), d1
and d2
, initialized to empty or 0.
Iteration: Iterate through both strings s
and t
simultaneously using zip
(Python) or indexing (other languages).
Mapping Check: For each pair of characters (a, b)
from s
and t
, respectively:
a
is already mapped in d1
. If it is, verify that its mapping is b
. If not, return False
(violates one-to-one mapping).b
is already mapped in d2
. If it is, verify that its mapping is a
. If not, return False
.d1
and d2
: d1[a] = b
and d2[b] = a
.Return True: If the loop completes without returning False
, it means all characters have consistent mappings, and the strings are isomorphic. Return True
.
The provided code examples demonstrate this approach using different programming languages. Note that the array-based solutions are generally more efficient than the hash map-based solutions for this problem.
Python (Array-based):
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
d1, d2 = [0] * 256, [0] * 256
for i, (a, b) in enumerate(zip(s, t), 1):
a, b = ord(a), ord(b)
if d1[a] != d2[b]:
return False
d1[a] = d2[b] = i
return True
Java (Array-based):
class Solution {
public boolean isIsomorphic(String s, String t) {
int[] d1 = new int[256];
int[] d2 = new int[256];
int n = s.length();
for (int i = 0; i < n; ++i) {
char a = s.charAt(i), b = t.charAt(i);
if (d1[a] != d2[b]) {
return false;
}
d1[a] = i + 1;
d2[b] = i + 1;
}
return true;
}
}
(Other language examples are similar in structure, using arrays or hash maps as appropriate.)
This detailed explanation, along with the provided code examples, should give a comprehensive understanding of how to solve the "Isomorphic Strings" problem efficiently. The array-based solutions offer a slight performance advantage over hash map solutions in this specific problem due to the fixed and limited size of the character set.