[
string
groupby
]
BinarySearch 0499 Social Distancing
Problem statement
https://binarysearch.com/problems/Social-Distancing/
Solution
Use groupby and check condition for every border and middle groups.
Complexity
It is O(n)
for time and space.
Code
class Solution:
def solve(self, s, k):
I = []
for x, y in groupby(s):
if x == ".": I += [len(list(y))]
if s[0] == "x": I = [0] + I
if s[-1] == "x": I = I + [0]
if I[0] >= k or I[-1] >= k: return True
if any(x >= 2*k - 1 for x in I[1:-1]): return True
return False