Problem : https://leetcode.com/problems/isomorphic-strings/
Because we can use any character to update s. Only the relative position of characters matters.
Use the first pos of one character to encode the input string. Then check if the 2 encoded strings are identical.
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
def encode(word):
seen = {}
for i, x in enumerate(word):
if x not in seen:
seen[x] = i
yield seen[x]
return all(map(lambda x: x[0] == x[1], zip(encode(s), encode(t))))
Updated on 07/12/2021. Update for a simpler solution.
No comments:
Post a Comment