forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-if-a-string-can-break-another-string.py
57 lines (45 loc) · 1.29 KB
/
check-if-a-string-can-break-another-string.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
# Time: O(n)
# Space: O(1)
import collections
import string
class Solution(object):
def checkIfCanBreak(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: bool
"""
def is_break(count1, count2):
curr1, curr2 = 0, 0
for c in string.ascii_lowercase:
curr1 += count1[c]
curr2 += count2[c]
if curr1 < curr2:
return False
return True
count1, count2 = collections.Counter(s1), collections.Counter(s2)
return is_break(count1, count2) or is_break(count2, count1)
# Time: O(nlogn)
# Space: O(1)
import itertools
class Solution2(object):
def checkIfCanBreak(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: bool
"""
return not {1, -1}.issubset(set(cmp(a, b) for a, b in itertools.izip(sorted(s1), sorted(s2))))
# Time: O(nlogn)
# Space: O(1)
import itertools
class Solution3(object):
def checkIfCanBreak(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: bool
"""
s1, s2 = sorted(s1), sorted(s2)
return all(a >= b for a, b in itertools.izip(s1, s2)) or \
all(a <= b for a, b in itertools.izip(s1, s2))