Check if a binary string contains consecutive same or not

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.

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

Invalid


My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.