forked from black-shadows/LeetCode-Topicwise-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-if-string-is-decomposable-into-value-equal-substrings.py
69 lines (64 loc) · 1.72 KB
/
check-if-string-is-decomposable-into-value-equal-substrings.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
# Time: O(n)
# Space: O(1)
class Solution(object):
def isDecomposable(self, s):
"""
:type s: str
:rtype: bool
"""
if len(s)%3 != 2:
return False
for left in xrange(0, len(s), 3):
if any(s[i] != s[i-1] for i in xrange(left+1, min(left+3, len(s)))):
break
for right in reversed(xrange(left+1, len(s), 3)):
if any(s[i] != s[i+1] for i in reversed(xrange(max(right-2, left), right))):
break
return right-left == 1
# Time: O(n)
# Space: O(1)
class Solution2(object):
def isDecomposable(self, s):
"""
:type s: str
:rtype: bool
"""
found, i = False, 0
while i < len(s):
l = 1
for j in xrange(i+1, min(i+3, len(s))):
if s[j] != s[i]:
break
l += 1
if l < 2:
return False
if l == 2:
if found:
return False
found = True
i += l
return found
# Time: O(n)
# Space: O(1)
class Solution3(object):
def isDecomposable(self, s):
"""
:type s: str
:rtype: bool
"""
found, l = False, 0
for i, c in enumerate(s):
if not l or c != s[i-1]:
if l:
return False
l = 1
continue
l += 1
if l == 2:
if i == len(s)-1 or s[i] != s[i+1]:
if found:
return False
found, l = True, 0
elif l == 3:
l = 0
return found