Program to check whether we can stand at least k distance away from the closest contacts in Python

Suppose we have a string s and a number k. Each character in the string is either a dot ('.') or 'x', where dot indicates an empty space and 'x' indicates a person. We need to check whether it's possible to choose a position to stand on such that the distance between us and the closest person is at least k units away.

The distance between neighboring indices is 1. For example, if the input is s = "x...x.." and k = 2, the output will be True because we can stand at position s[2] or s[6].

Algorithm

To solve this problem, we follow these steps ?

  • Find the first occurrence of 'x' in the string
  • If no person exists or the first person is far enough from the start, return True
  • Check gaps between consecutive people to see if any gap allows us to maintain distance k
  • Check if we can stand at the end of the string with sufficient distance from the last person

Example

class Solution:
    def solve(self, s, k):
        # Find first occurrence of 'x'
        pos = s.find("x")
        
        # If no person exists or first person is far enough from start
        if pos == -1 or pos >= k:
            return True
        
        last_x = pos
        dist_min = 2 * k - 1  # Minimum gap needed between two people
        
        while True:
            # Find next occurrence of 'x'
            next_x = s.find("x", last_x + 1)
            
            if next_x != -1:
                # Check if gap between people is large enough
                if next_x - last_x - 1 >= dist_min:
                    return True
                last_x = next_x
            else:
                # Check if we can stand at the end
                if len(s) - last_x - 1 >= k:
                    return True
                return False
        
        return False

# Test the solution
ob = Solution()
print(ob.solve("x...x..", 2))
print(ob.solve("x.x", 2))
print(ob.solve("..x..", 1))

The output of the above code is ?

True
False
True

How It Works

The algorithm works by checking three possible scenarios where we can maintain distance k ?

  • Beginning: If the first person is at position k or later, we can stand at the start
  • Between people: If there's a gap of at least 2k-1 spaces between two people, we can stand in the middle
  • End: If there are at least k empty spaces after the last person, we can stand at the end

Key Points

  • The minimum gap needed between two people is 2k-1 to maintain distance k from both
  • We only need k spaces at the beginning or end since there's only one person nearby
  • The algorithm uses string's find() method to locate people efficiently

Conclusion

This solution efficiently checks if we can maintain a safe distance k from all people by examining gaps at the beginning, between people, and at the end of the string. The time complexity is O(n) where n is the number of people in the string.

---
Updated on: 2026-03-25T10:31:17+05:30

151 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements