0% completed
Problem Statement
Given two strings s and t, return true if they are isomorphic. Otherwise, return false.
Two strings s and t are called isomorphic if the characters in string s can be replaced to get string t.
Each character in string t should be replaced with another character while preserving the order of characters as in string s. Two characters should not map to the same character, but a character may map to itself.
Examples
-
Example 1:
- Input: s = "abb", t = "cdd"
- Expected Output:
true - Justification: Each character in "abb" can be replaced to match the corresponding character in "cdd" (a->c, b->d) while maintaining a one-to-one mapping.
-
Example 2:
- Input: s = "cbcrt", t = "abaxv"
- Expected Output:
true - Justification: The characters in "cbcrt" can be replaced to form "abaxv" with a unique mapping for each character (c->a, b->b, r->x, t->v).
-
Example 3:
- Input: s = "abcd", t = "bbcd"
- Expected Output:
false - Justification: The first string's characters cannot be replaced to form the string
twhile maintaining a unique mapping, as 'b' in the second string corresponds to both 'a' and 'b' in thes.
Solution
To solve this problem, we'll use a mapping strategy to track the transformation of characters from the first string to the second. This approach works by ensuring that each character in the first string can uniquely map to a character in the second string, and vice versa. The reason this method is effective lies in its ability to maintain a one-to-one correspondence between the characters of both strings, which is crucial for the strings to be isomorphic. By using two hash maps (or dictionaries) to store these mappings in both directions, we can efficiently check the conditions for isomorphism. This dual-mapping technique allows us to verify that no two characters in the first string map to the same character in the second string and that the mapping is consistent throughout both strings.
Step-by-step Algorithm
-
Initialize Two Hash Maps: Start by creating two empty hash maps (or dictionaries). The first map (
mapS2T) will store mappings from characters in stringsto stringt. The second map (mapT2S) will store mappings in the opposite direction, from characters in stringtto strings. -
Iterate Over Characters: Loop through each character of the strings
sandtsimultaneously. For each positioni, get the characterscharSfromsandcharTfromt. -
Check and Update Mappings:
- For each character pair (
charS,charT), perform the following checks:- If
charSis already mapped inmapS2T, check if it maps to the currentcharT. If not, the strings are not isomorphic, returnfalse. - Similarly, if
charTis already mapped inmapT2S, check if it maps to the currentcharS. If not, returnfalse.
- If
- If both checks pass, update
mapS2Tby mappingcharStocharTandmapT2Sby mappingcharTtocharS.
- For each character pair (
-
Return True: If the loop completes without finding any inconsistencies in the mappings, return
true. This means that the strings are isomorphic.
Algorithm Walkthrough
Let's consider the input: s = "cbcrt", t = "abaxv"
-
Initial State:
mapS2T = {},mapT2S = {} -
Step 1: (
c,a)mapS2Tdoes not containc, andmapT2Sdoes not containa.- Update maps:
mapS2T = {c: a},mapT2S = {a: c}
-
Step 2: (
b,b)mapS2Tdoes not containb, andmapT2Sdoes not containb.- Update maps:
mapS2T = {c: a, b: b},mapT2S = {a: c, b: b}
-
Step 3: (
c,a)mapS2Tcontainscmapping toa, andmapT2Scontainsamapping toc. The mappings are consistent.- No update needed. Maps remain:
mapS2T = {c: a, b: b},mapT2S = {a: c, b: b}
-
Step 4: (
r,x)mapS2Tdoes not containr, andmapT2Sdoes not containx.- Update maps:
mapS2T = {c: a, b: b, r: x},mapT2S = {a: c, b: b, x: r}
-
Step 5: (
t,v)mapS2Tdoes not containt, andmapT2Sdoes not containv.- Update maps:
mapS2T = {c: a, b: b, r: x, t: v},mapT2S = {a: c, b: b, x: r, v: t}
-
Final State: After iterating through all character pairs, the mappings in both
mapS2TandmapT2Sare consistent and complete without any conflicts. Thus, returntrue.
Code
Complexity Analysis
Time Complexity: O(N) - where N is the length of the strings. The algorithm iterates over each character of the strings exactly once to check or create a mapping.
Space Complexity: O(1) - The space used by the hash maps does not depend on the size of the input strings directly since the number of unique characters that can be stored is limited by the character set (e.g., ASCII has 128 unique characters, extended ASCII has 256). Therefore, the space complexity can be considered as O(1) in terms of the input size.
On this page
Problem Statement
Examples
Solution
Step-by-step Algorithm
Algorithm Walkthrough
Code
Complexity Analysis