easy Isomorphic Strings
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.
Try it yourself
Try solving this question here:
✅ Solution Isomorphic Strings
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
import java.util.HashMap;
public class Solution {
public boolean isIsomorphic(String s, String t) {
HashMap<Character, Character> mapS2T = new HashMap<>();
HashMap<Character, Character> mapT2S = new HashMap<>();
// Iterate through each character in both strings
for (int i = 0; i < s.length(); i++) {
char charS = s.charAt(i);
char charT = t.charAt(i);
// Check if there is a previous mapping and if it matches the current character
if (
(mapS2T.containsKey(charS) && mapS2T.get(charS) != charT) ||
(mapT2S.containsKey(charT) && mapT2S.get(charT) != charS)
) {
return false; // Mappings do not match, strings are not isomorphic
}
// Add or update the character mapping
mapS2T.put(charS, charT);
mapT2S.put(charT, charS);
}
return true; // All mappings are consistent, strings are isomorphic
}
public static void main(String[] args) {
Solution solution = new Solution();
// Example 1
System.out.println(solution.isIsomorphic("abb", "cdd")); // true
// Example 2
System.out.println(solution.isIsomorphic("cbcrt", "abaxv")); // true
// Example 3
System.out.println(solution.isIsomorphic("abcd", "bbcd")); // false
}
}
Complexity Analysis
Time Complexity:
Space Complexity:
🤖 Don't fully get this? Learn it with Claude
Stuck on Isomorphic Strings? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Isomorphic Strings** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Isomorphic Strings** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Isomorphic Strings**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Isomorphic Strings**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.