- Notifications
You must be signed in to change notification settings - Fork 625
/
Copy path990.py
46 lines (39 loc) · 1.72 KB
/
990.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
'''
Given an array equations of strings that represent relationships between variables, each string equations[i] has length 4 and takes one of two different forms: "a==b" or "a!=b". Here, a and b are lowercase letters (not necessarily different) that represent one-letter variable names.
Return true if and only if it is possible to assign integers to variable names so as to satisfy all the given equations.
Example 1:
Input: ["a==b","b!=a"]
Output: false
Explanation: If we assign say, a = 1 and b = 1, then the first equation is satisfied, but not the second. There is no way to assign the variables to satisfy both equations.
'''
classSolution(object):
defequationsPossible(self, equations):
"""
:type equations: List[str]
:rtype: bool
"""
equal_list, unequal_list= [], []
forequationinequations:
x, y=equation[0], equation[3]
if'=='inequation:
ifnotequal_list:
equal_list.append(x+y)
else:
found=False
forindexinrange(0, len(equal_list)):
val=equal_list[index]
ifxinvaloryinval:
val=val+x+y
equal_list[index] =val
found=True
ifnotfound:
equal_list.append(x+y)
else:
ifx==y:
returnFalse
unequal_list.append([x, y])
forvalinunequal_list:
forequalinequal_list:
ifval[0] inequalandval[1] inequal:
returnFalse
returnTrue