Skip to content

205. Isomorphic Strings #111

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We can use two hash maps (or associative arrays in PHP) to track the mappings of characters from s to t and vice versa.

Let's implement this solution in PHP: 205. Isomorphic Strings

<?php
function isIsomorphic($s, $t) {
    if (strlen($s) != strlen($t)) {
        return false;
    }

    $mapST = [];
    $mapTS = [];

    for ($i = 0; $i < strlen($s); $i++) {
        $charS = $s[$i];
        $charT = $t[$i];

        // Check if there is a mapping for charS to charT
        if (isset($mapST[$charS])) {
            if ($mapST[$charS] != $charT) {
                return false;
            }
        } else {
            $mapST[$charS] = $charT;
        }

        // Check if there is a mappi…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@mah-shamim
Comment options

mah-shamim Aug 21, 2024
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
1 participant