Tensorum
YouTubeనేర్చుకోవడం ప్రారంభించండి
LeetCode 205 · Hashing / Frequency

Isomorphic Strings

LeetCode 205 · సులభం · పరిష్కారం, intuition & దశలవారీ వాక్‌త్రూ

Compare 'last seen index' of s[i] and t[i].

సులభంHash TableString
LeetCode లో తెరవండి ↗

నేను Isomorphic Strings (LeetCode 205) ను ఇలా పరిష్కరించాను — Hashing / Frequency ప్యాటర్న్‌లోని ఒక సులభం ప్రాబ్లమ్. కింద: intuition, దశలవారీ approach, నా accepted Python కోడ్, animated test cases, line-by-line debugger.

కింది వివరణ ఇంగ్లీష్‌లో ఉంది.

Intuition

Two strings are isomorphic if each character's pattern of appearances matches. Record the last index where each character appeared in s and in t; at every position those last indices must agree.

Approach

  1. 1

    Two maps: last index per character for s and for t.

  2. 2

    If the stored indices differ, the mapping breaks.

  3. 3

    Update both with the current index.

నేను ఎలా పరిష్కరించాను

Last-seen indicesPythonO(n) time
class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        #if two letters from s and t match correctly, their previous occurance must match
        s_mpp = {}
        t_mpp = {}
        for i in range(len(s)):
            char_s = s[i]
            char_t = t[i]
            if s_mpp.get(char_s,-1)!=t_mpp.get(char_t,-1):
                return False
            s_mpp[char_s]=i
            t_mpp[char_t]=i
        return True
స్టెప్-త్రూ లోడ్ అవుతోంది…

Isomorphic Strings — తరచూ అడిగే ప్రశ్నలు

Isomorphic Strings వెనుక ఉన్న intuition ఏమిటి?

Two strings are isomorphic if each character's pattern of appearances matches. Record the last index where each character appeared in s and in t; at every position those last indices must agree.

Isomorphic Strings (LeetCode 205) ను ఎలా పరిష్కరించాలి?

1. Two maps: last index per character for s and for t. 2. If the stored indices differ, the mapping breaks. 3. Update both with the current index.

Isomorphic Strings యొక్క time, space complexity ఎంత?

Time: O(n). Space: O(1) — bounded alphabet. (Accepted approach: Last-seen indices.)

Isomorphic Strings లో సాధారణ పొరపాట్లు ఏమిటి?

Checking only one direction misses cases like 'ab' → 'aa'.