Skip to content

Commit 13f76e0

Browse files
committed
Time: 95 ms (62.08%), Space: 18 MB (99.63%) - LeetHub
1 parent 830935c commit 13f76e0

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# time complexity: O(n)
2+
# space complexity: O(1)
3+
class Solution:
4+
def countCollisions(self, directions: str) -> int:
5+
result = 0
6+
flag = -1
7+
8+
for dir in directions:
9+
if dir == "L":
10+
if flag >= 0:
11+
result += flag + 1
12+
flag = 0
13+
elif dir == "S":
14+
if flag > 0:
15+
result += flag
16+
flag = 0
17+
else:
18+
if flag >= 0:
19+
flag += 1
20+
else:
21+
flag = 1
22+
return result
23+
24+
25+
directions = "RLRSLL"
26+
print(Solution().countCollisions(directions))
27+
directions = "LLRR"
28+
print(Solution().countCollisions(directions))

0 commit comments

Comments
 (0)