Suppose we have a string s and a number k. Now each character in the string is either dot ('.') or 'x', where dot indicates an empty space and 'x' indicates a person. We have to check whether it's possible to choose a position to stand on such that the distance between us and the closest person to us is at least k. (Here the distance between each neighbouring indices is 1).
So, if the input is like s = "x...x..", k = 2, then the output will be True, as we can stand at s or s.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, s, k): pos = s.find("x") if pos==-1 or pos>=k: return True last_x = pos dist_min = 2*k-1 while True: next_x = s.find("x", last_x+1) if next_x!=-1: if next_x-last_x-1 >= dist_min: return True last_x = next_x else: if len(s)-last_x-1>=k: return True return False return None ob = Solution() print(ob.solve("x...x..", 2))