Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if all the 1s in a binary string are equidistant or not in Python
Suppose we have a binary string, we need to check whether all the 1s in the string are equidistant or not. In other words, the distance between every two consecutive 1s should be the same, and the string must contain at least two 1s.
So, if the input is like s = "100001000010000", then the output will be True as the 1s are at positions 0, 5, 10 with equal distances of 5 between them.
Algorithm
To solve this problem, we will follow these steps ?
- Find all indices where '1' appears in the string
- Calculate the distance between the first two 1s
- Check if all consecutive pairs of 1s have the same distance
- Return True if all distances are equal, False otherwise
Example
Let's implement this solution step by step ?
def solve(s):
indices = []
# Find all positions of '1' in the string
for i in range(len(s)):
if s[i] == '1':
indices.append(i)
# If less than 2 ones, return True (trivially equidistant)
if len(indices) < 2:
return True
# Calculate expected distance from first two 1s
expected_distance = indices[1] - indices[0]
# Check if all consecutive distances are equal
for i in range(1, len(indices)):
if (indices[i] - indices[i - 1]) != expected_distance:
return False
return True
# Test the function
s = "100001000010000"
result = solve(s)
print(f"Input: {s}")
print(f"Are all 1s equidistant? {result}")
Input: 100001000010000 Are all 1s equidistant? True
Testing with Different Cases
Let's test our solution with various binary strings to see how it works ?
def solve(s):
indices = []
for i in range(len(s)):
if s[i] == '1':
indices.append(i)
if len(indices) < 2:
return True
expected_distance = indices[1] - indices[0]
for i in range(1, len(indices)):
if (indices[i] - indices[i - 1]) != expected_distance:
return False
return True
# Test cases
test_cases = [
"100001000010000", # Equidistant (distance = 5)
"101010", # Equidistant (distance = 2)
"100100100", # Equidistant (distance = 3)
"1001010", # Not equidistant
"1100110011" # Not equidistant
]
for s in test_cases:
result = solve(s)
print(f"'{s}' ? {result}")
'100001000010000' ? True '101010' ? True '100100100' ? True '1001010' ? False '1100110011' ? False
How It Works
The algorithm works by first collecting all positions where '1' appears in the string. Then it uses the distance between the first two 1s as the expected distance and compares all subsequent consecutive distances against this reference.
For the string "100001000010000", the 1s are at positions [0, 5, 10]. The distances are [5, 5], which are equal, so the function returns True.
Conclusion
This solution efficiently checks if all 1s in a binary string are equidistant by finding their positions and comparing consecutive distances. The time complexity is O(n) where n is the length of the string.
