Check if a binary string contains consecutive same or not Last Updated : 06 Dec, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 the same. Examples: Input: str[] = "010101" Output: Valid Input: str[] = "010010" Output: Invalid 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: C++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true is str is valid bool isValid(string str, int len) { // Assuming the string is binary // If any two consecutive characters are equal // then the string is invalid for (int i = 1; i < len; i++) { if (str[i] == str[i - 1]) return false; } // If the string is alternating return true; } // Driver code int main() { string str = "0110"; int len = str.length(); if (isValid(str, len)) cout << "Valid"; else cout << "Invalid"; return 0; } Java // Java implementation of the approach class gfg { // Function that returns true is str is valid static boolean isValid(String str, int len) { // Assuming the string is binary // If any two consecutive // characters are equal // then the string is invalid for (int i = 1; i < len; i++) { if (str.charAt(i) == str.charAt(i - 1)) return false; } // If the string is alternating return true; } // Driver code public static void main(String[] args) { String str = "0110"; int len = str.length(); if (isValid(str, len)) System.out.println("Valid"); else System.out.println("Invalid"); } } // This code is Contributed by Code_Mech Python3 # Python3 implementation of the approach # Function that returns true is str is valid def isValid(string, length): # Assuming the string is binary # If any two consecutive characters # are equal then the string is invalid for i in range(1, length): if string[i] == string[i - 1]: return False # If the string is alternating return True # Driver code if __name__ == "__main__": string = "0110" length = len(string) if isValid(string, length): print("Valid") else: print("Invalid") # This code is contributed by Rituraj Jain C# // C# implementation of the approach using System; class gfg { // Function that returns true is str is valid static bool isValid(string str, int len) { // Assuming the string is binary // If any two consecutive // characters are equal // then the string is invalid for (int i = 1; i < len; i++) { if (str[i] == str[i - 1]) return false; } // If the string is alternating return true; } // Driver code public static void Main() { string str = "0110"; int len = str.Length; if (isValid(str, len)) Console.Write("Valid"); else Console.Write("Invalid"); } } // This code is contributed by Ita_c. PHP <?php // PHP implementation of the approach // Function that returns true is str is valid function isValid($str, $len) { // Assuming the string is binary // If any two consecutive characters // are equal then the string is invalid for ($i = 1; $i < $len; $i++) { if ($str[$i] == $str[$i - 1]) return false; } // If the string is alternating return true; } // Driver code $str = "0110"; $len = strlen($str); if (isValid($str, $len)) echo "Valid"; else echo "Invalid"; // This code is contributed by Ryuga ?> JavaScript <script> // Javascript implementation of the approach // Function that returns true is str is valid function isValid(str,len) { // Assuming the string is binary // If any two consecutive // characters are equal // then the string is invalid for (let i = 1; i < len; i++) { if (str[i] == str[i - 1]) return false; } // If the string is alternating return true; } // Driver code let str = "0110"; let len = str.length; if (isValid(str, len)) document.write("Valid"); else document.write("Invalid"); // This code is contributed by rag2127 </script> OutputInvalid Time Complexity: O(N), where N is the length of the string.Auxiliary Space: O(1), As constant extra space is used. Comment More infoAdvertise with us Next Article Check if the string contains consecutive letters and each letter occurs exactly once S Shivam.Pradhan Follow Improve Article Tags : DSA binary-string array-traversal-question Similar Reads Check if a binary string contains all permutations of length k Given a binary string and k, to check whether it's contains all permutations of length k or not. Examples: Input : Binary string 11001 k : 2 Output : Yes 11001 contains all possibilities of binary sequences with k = 2, 00, 01, 10, 11 Input : Binary string: 1001 k : 2 Output: No 1001 does not contain 13 min read Count number of binary strings without consecutive 1's Given a positive integer n, the task is to count all possible distinct binary strings of length n such that there are no consecutive 1's.Examples: Input: n = 3Output: 5Explanation: 5 strings are ("000", "001", "010", "100", "101").Input: n = 2Output: 3Explanation: 3 strings are ("00", "01", "10").Ta 15+ min read Check if given Binary string follows then given condition or not Given binary string str, the task is to check whether the given string follows the below condition or not: String starts with a '1'.Each '1' is followed by empty string(""), '1', or "00".Each "00" is followed by empty string(""), '1'. If the given string follows the above criteria then print "Valid 10 min read Python - Check if there are K consecutive 1's in a binary number The task is to check if a binary number (a string of 0s and 1s) has k consecutive 1s in it. The goal is to figure out if this pattern exists in the easiest and fastest way possible. Using for loopThis method works by going through the string once and keeping track of how many '1's appear in a row. I 3 min read Check if the string contains consecutive letters and each letter occurs exactly once Given string str. The task is to check if the string contains consecutive letters and each letter occurs exactly once. Examples: Input: str = "fced" Output: YesThe string contains 'c', 'd', 'e' and 'f' which are consecutive letters. Input: str = "xyz" Output: Yes Input: str = "abd" Output: No Approa 15 min read Check if two strings are same or not Given two strings, the task is to check if these two strings are identical(same) or not. Consider case sensitivity.Examples:Input: s1 = "abc", s2 = "abc" Output: Yes Input: s1 = "", s2 = "" Output: Yes Input: s1 = "GeeksforGeeks", s2 = "Geeks" Output: No Approach - By Using (==) in C++/Python/C#, eq 7 min read Like