Determine whether two strings can be transformed into each other with a one-to-one character mapping.
Given two strings s and t, determine whether they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t with a consistent mapping.
- Each character in
smust map to exactly one character int. - No two different characters in
smay map to the same character int. - The mapping must preserve the order of characters.
Return true if the strings are isomorphic, otherwise return false.
Input Format
Two strings s and t.
Output Format
Return a boolean indicating whether the strings are isomorphic.
Constraints
Assume both strings consist of ASCII characters unless otherwise specified by the platform. The strings are typically of equal length for a valid isomorphism check.
Example 1
Input
s = "egg", t = "add"
Output
true
Explanation
The mapping can be e -> a and g -> d.
Example 2
Input
s = "foo", t = "bar"
Output
false
Explanation
The character o would need to map to both a and r, which is not allowed.
Show 1 more example
Example 3
Input
s = "paper", t = "title"
Output
true
Explanation
A consistent one-to-one mapping exists: p -> t, a -> i, e -> l, r -> e.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.