Given a binary string str consisting of characters ‘0’ and ‘1’. The task is to find whether the string is valid or not. A string is valid only if the characters are alternating i.e. no two consecutive characters are same.
Input: str = “010101”
Input: str = “010010”
Approach: Start traversing the string character by character and if there are any two consecutive characters that are equal then print Invalid else print Valid in the end.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a binary string has two consecutive occurrences of one everywhere
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Python | Check if there are K consecutive 1's in a binary number
- Check if a string has m consecutive 1's or 0's
- Length of longest consecutive ones by at most one swap in a Binary String
- Maximum Consecutive Zeroes in Concatenated Binary String
- Maximum length of consecutive 1's in a binary string in Python using Map function
- Binary string with given frequencies of sums of consecutive pairs of characters
- Check if string is palindrome after removing all consecutive duplicates
- Check if the string contains consecutive letters and each letter occurs exactly once
- Check if binary representations of 0 to N are present as substrings in given binary string
- Python | Check if a given string is binary string or not
- Check divisibility of binary string by 2^k
- Check if all the 1's in a binary string are equidistant or not
- Check if given Binary string follows then given condition or not
- Check if a binary string contains all permutations of length k
- Check if a binary string has a 0 between 1s or not | Set 1 (General approach)
- Check if it is possible to rearrange a binary string with alternate 0s and 1s
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Check whether the binary equivalent of a number ends with given string or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.