forked from HuberTRoy/leetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IsomorphicStrings.py
119 lines (96 loc) · 3.32 KB
/
IsomorphicStrings.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
Given two strings s and t, determine if they are isomorphic.
Two strings 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
Note:
You may assume both s and t have the same length.
给定两个字符串,判断是否属于相同的模式。
这个没想到什么好办法,就是根据字典进行替换然后对比是否一致。
在Discuss里看到一个 one line 版。
先上自己的思路:
迭代字符串a,如果遇到的是a_d中的字符串,则根据a_d替换,否则将其根据出现的顺序添加到a_d中,
这样做让每个单词根据字符所出现的顺序进行字典替换。
测试用例:
https://leetcode.com/problems/isomorphic-strings/description/
"""
class Solution(object):
def isIsomorphic(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
"""
这里可以缩写为一个函数,但不需要复用,直接复制粘贴了。
"""
s_k = 0
t_k = 0
s_d = {}
t_d = {}
new_s = ''
new_t = ''
for i in s:
if s_d.get(i):
new_s += s_d.get(i)
else:
s_d[i] = str(s_k)
s_k += 1
for i in t:
if t_d.get(i):
new_t += t_d.get(i)
else:
t_d[i] = str(t_k)
t_k += 1
return new_s == new_t
"""
接下来是one line 版,虽然并不是很高效,但思路不错。
这个问题的核心问题是 重复的数据,如果每个单词都不想同那么也是同一种模式,关键就是找出相同的单词。
zip会将每个部位的单词两两对应。
`zip` `rar`
('z', 'r'), ('i', 'a'), ('p', 'r')
如果两个单词的模式相同,那么一定会出现多组同样的数据,且数量与原单词去重后一致。
t,s与s,t的效果应该是一致的,都不会影响判断。
return len(set(zip(s, t))) == len(set(s)) and len(set(zip(t, s))) == len(set(t))
"""
"""
第三版:
根据 one line 的思路:
zip可以有效检测分组:
one line 虽然简单易写,不过有3次set,时间复杂度为 O(3n),set是c++写的所以很快,感觉不出来。
就是把第一版变成了zip的的形式,理论上应该会快的,但实际运行时间与第一版一致。
"""
class Solution(object):
def isIsomorphic(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
s_k = 0
t_k = 0
s_d = {}
t_d = {}
for i in zip(s, t):
if s_d.get(i[0]):
s_k2 = s_d.get(i[0])
else:
s_k2 = s_d[i[0]] = str(s_k)
s_k += 1
if t_d.get(i[1]):
t_k2 = t_d.get(i[1])
else:
t_k2 = t_d[i[1]] = str(t_k)
# t_k2 = t_d.get(i[1])
t_k += 1
if s_k2 != t_k2:
return False
return True