Skip to content

Instantly share code, notes, and snippets.

@Ephraimiyanda
Last active February 1, 2026 23:42
Show Gist options
  • Select an option

  • Save Ephraimiyanda/626229a996d145821b896a4cfca5d5bd to your computer and use it in GitHub Desktop.

Select an option

Save Ephraimiyanda/626229a996d145821b896a4cfca5d5bd to your computer and use it in GitHub Desktop.
Isomorphic Strings

Question

Approach

I create two maps to check if the order of characters are preserved. t is used as pattern for s and s also a pattern for t. if the two patterns d not match then we return false

Complexity

  • Time complexity: O(N)

  • Space complexity: O(N)

Code

function isIsomorphic(s: string, t: string): boolean {
    let map = new Map()
    let reverseMap = new Map()
    let sArr = s.split("")
    let tArr = t.split("")
    if (s.length !== t.length) return false
    for (let i = 0; i < sArr.length; i++) {
        let a = sArr[i]
        let b = tArr[i]
        if ((map.has(a) && map.get(a) !== b)
            || (reverseMap.has(b) && reverseMap.get(b) !== a)) {
            return false
        }
        if (!map.has(a) || !reverseMap.has(b)) {
            map.set(a, b)
            reverseMap.set(b, a)
        }
    }
    return true
};
scrnli_9pqztwCeodXJ7K
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment