Isomorphic Strings
LeetCode 205 · आसान · समाधान, intuition और कदम-दर-कदम वॉकथ्रू
Compare 'last seen index' of s[i] and t[i].
मैंने 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
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.
मैंने कैसे हल किया
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 TrueIsomorphic 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'.