Isomorphic Strings
Problem statement
Given two strings s and t of equal length, determine whether their characters have a bijective mapping.
A valid mapping must satisfy both rules:
- Every occurrence of one character in
smaps to the same character int. - Two different characters in
scannot map to the same character int.
Character positions do not change, and a character may map to itself.
Function
isIsomorphic(s: String, t: String) → booleanExamples
Example 1
s = "paper"t = "title"return = trueThe repeated-character pattern matches: p maps to t, a to i, e to l, and r to e.
Example 2
s = "foo"t = "bar"return = falseThe two occurrences of o would need to map to both a and r, which is inconsistent.
Example 3
s = "badc"t = "baba"return = falseAlthough each character from s is individually consistent, both b and d would map to b, violating bijection.
Constraints
1 <= s.length == t.length <= 50000sandtcontain ASCII characters.