-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisomorphic_strings_205.py
90 lines (70 loc) · 2.48 KB
/
isomorphic_strings_205.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Given two strings s and t, determine if they are isomorphic.
# Two strings s and t are isomorphic if the characters in s can be replaced to get t.
# All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
# Example 1:
# Input: s = "egg", t = "add"
# Output: true
# Example 2:
# Input: s = "foo", t = "bar"
# Output: false
# Example 3:
# Input: s = "paper", t = "title"
# Output: true
# https://leetcode.com/problems/isomorphic-strings/
# My solution
# ---------------------------------------Runtime 47 ms Beats 53.81% Memory 14.2 MB Beats 100%---------------------------------------
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
if len(set(s)) != len(set(t)):
return False
temp_dict = {}
for i in range(len(s)):
if s[i] not in temp_dict and t[i] not in temp_dict.values():
temp_dict[s[i]] = t[i]
elif s[i] not in temp_dict and t[i] in temp_dict.values():
return False
elif s[i] in temp_dict and temp_dict[s[i]] != t[i]:
return False
return True
# NeetCode's solution
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
mapSt, mapTs = {}, {}
for c1, c2 in zip(s, t):
if (c1 in mapSt and mapSt[c1] != c2) or (
c2 in mapTs and mapTs[c2] != c1
):
return False
mapSt[c1] = c2
mapTs[c2] = c1
return True
if __name__ == "__main__":
solution = Solution()
s = "abcdefghijklmnopqrstuvwxyzva"
t = "abcdefghijklmnopqrstuvwxyzck"
s = "bbbaaaba"
t = "aaabbbba" # false
# s = "aba"
# t ="aab" # false
print(solution.isIsomorphic(s, t))
# class Solution:
# def isIsomorphic(self, s: str, t: str) -> bool:
# if len(s) != len(t):
# return False
# set_1 = set(s)
# set_2 = set(t)
# if len(set_1) == len(set_2):
# return True
# return False
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
for i in range(1, len(s)):
if s[i - 1] == s[i] and t[i - 1] != t[i]:
return False
elif s[i - 1] != s[i] and t[i - 1] == t[i]:
return False
else:
continue
return True