forked from super30admin/Hashing-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem3.py
44 lines (34 loc) · 1020 Bytes
/
problem3.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
# Concept is same as ismorphic strings.
# Time Complexity: O(n)
# Space Complexity: O(1)
def check_pattern(pattern, string):
pattern_dict = {}
seen_set = set()
split_str = string.split(' ')
if len(split_str) != len(pattern):
return False
for i in range(len(pattern)):
if pattern[i] in pattern_dict:
if split_str[i] == pattern_dict[pattern[i]]:
continue
else:
return False
else:
if split_str[i] in seen_set:
return False
else:
pattern_dict[pattern[i]] = split_str[i]
seen_set.add(split_str[i])
return True
pattern = "abba"
string = "dog cat cat dog"
print(check_pattern(pattern, string))
pattern = "abba"
string = "dog cat cat fish"
print(check_pattern(pattern, string))
pattern = 'aaaa'
string = "dog cat cat dog"
print(check_pattern(pattern, string))
pattern = "abba"
string = "dog dog dog dog"
print(check_pattern(pattern, string))