Horje
Rearrange a Binary String with Alternating 0s and 1s in Python

We are given a binary string and we have to check whether it is possible to rearrange the string with alternate 0s and 1s. Below are a few examples to understand the problem statement clearly.

Examples:

Input: "1011"
Output: False
Explanation: We can’t rearrange the string such that it has alternate 0s and 1s.

Input: 1100
Output: YES
Explanation: There are exactly two ways to rearrange the string, and they are 0101 or 1010.

Possibility to Rearrange a Binary String with Alternate 0s and 1s

Now let us see different approaches to check if it is possible to rearrange a binary string with alternate 0s and 1s in Python.

Using Count Function

Python string count() function is used to count the occurrence of a character in a string. It take one argument and returns an integer value. Using the count() function to count the number of s and 1s, compute the absolute difference between them. If the absolute difference is less than or equal to 1, that means the 0s and 1s can be rearranged alternatively.

Example: In this example, we first count number of 0s and 1s and then find the absolute difference using the abs() function.

Python
def can_rearrange_alternately(s):
    # Count the number of '0's and '1's in the string
    count_0 = s.count('0')
    count_1 = s.count('1')
    
    #  Check if the absolute difference between the counts is at most 1
    return abs(count_0 - count_1) <= 1

s = "11100"
print(can_rearrange_alternately(s))  

Output:

True

Using collection.Counter

In this method, the Python collection module‘s Counter class is used to count the number of characters in the string. The get() function is then used to count the occurrence of the character that is passed to it as an argument in the string. Lastly the absolute difference. using abs() function, between the 0s and 1s determines if the binary string can be rearranged with alternative 0s and 1s or not.

Example: In this example, we first import the Counter class from collections module. Then use counter class’s get() function to calculate the number of 0s and 1s and then find the absolute difference using the abs() function.

Python
from collections import Counter

def can_rearrange_alternately(s):
    # Count the occurrences of each character in the string
    counter = Counter(s)
    
    # Retrieve the counts of '0's and '1's
    count_0 = counter.get('0', 0)
    count_1 = counter.get('1', 0)
    
    return abs(count_0 - count_1) <= 1

s = "111001"
print(can_rearrange_alternately(s)) 

Output:

False



Reffered: https://www.geeksforgeeks.org


Python

Related
Getting Started with Conda Getting Started with Conda
Dependency resolution and lock files In Pyhton Poetry Dependency resolution and lock files In Pyhton Poetry
Understanding Versions, Ranges, and Constraints in Python Poetry Understanding Versions, Ranges, and Constraints in Python Poetry
Data Visualization Using Bqplot in Python Data Visualization Using Bqplot in Python
Python Version History Python Version History

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12