Skip to main content

205 - Isomorphic Strings

Details

KeyValue
Linkhttps://leetcode.com/problems/isomorphic-strings/
LanguagePython 3
Runtime68 ms, faster than 38.69% of Python3 online submissions for Isomorphic Strings
Memory Usage14.1 MB, less than 87.87% of Python3 online submissions for Isomorphic Strings
DatastructuresCache (seen hash)
Algorithms2 Dicts
ComplexityTime: O(n), Space: O(26)

Procedure

  1. ...

Code

class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
def is_mismatch():
if (
(s_char not in s_cache) or (s_cache[s_char] != t_char) or
(t_char not in t_cache) or (t_cache[t_char] != s_char)
): return True

s_cache = {}
t_cache = {}

for s_char, t_char in zip(s,t):
if s_char not in s_cache and t_char not in t_cache:
s_cache[s_char] = t_char
t_cache[t_char] = s_char

if is_mismatch():
return False

return True