← DiffPush

Isomorphic string

Baseline Strings · Easy O(n) · O(1)

Cipher Consistency Audit

A cipher vault replaces each plaintext letter with a ciphertext letter through a fixed substitution table, and an auditor receives two transcripts claimed to be encodings of the same message. The substitution must be consistent: one source letter always becomes the same target letter, and no two different source letters may collapse onto the same target. The auditor certifies the pair only when such a flawless mapping exists.

Input: Two strings s and t of equal length.

Output: true when the characters of s can be replaced to produce t under a consistent one-to-one mapping, false otherwise.

Constraints

Examples

Example 1

Input: {"s":"egg","t":"add"}
Output: true
e maps to a and g maps to d; both replacements hold at every occurrence.

Example 2

Input: {"s":"foo","t":"bar"}
Output: false
The two o's in s would need to become a and r simultaneously, which no single mapping allows.

Solve this in your browser →

Also on LeetCode ↗