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'.