Check if a binary string has two consecutive occurrences of one everywhere
Last Updated :
07 Mar, 2023
Given string str consisting of only the characters ‘a’ and ‘b’, the task is to check whether the string is valid or not. In a valid string, every group of consecutive b must be of length 2 and must appear after 1 or more occurrences of character ‘a’ i.e. “abba” is a valid sub-string but “abbb” and aba are not. Print 1 if the string is valid, else print -1.
Examples:
Input: str = “abbaaabbabba”
Output: 1
Input: str = “abbaaababb”
Output: -1
Approach: Find every occurrence of ‘b’ in the string and check whether it is a part of the sub-string “abb”. If the condition fails for any sub-string, then print -1 else print 1.
Algorithm:
C++
#include <bits/stdc++.h>
using namespace std;
bool isValidString(string str, int n)
{
int index = find(str.begin(),
str.end(), 'b' ) -
str.begin();
if (index == 0)
return false ;
while (index <= n - 1)
{
if (str[index - 1] != 'a' )
return false ;
if (index + 1 < n && str[index + 1] != 'b' )
return false ;
if (index + 2 < n && str[index + 2] == 'b' )
return false ;
if (index == n - 1)
return false ;
index = find(str.begin() + index + 2,
str.end(), 'b' ) - str.begin();
}
return true ;
}
int main()
{
string str = "abbaaabbabba" ;
int n = str.length();
isValidString(str, n) ? cout
<< "true" : cout << "false" ;
return 0;
}
|
Java
class GFG {
private static boolean isValidString(String str, int n)
{
int index = str.indexOf( "b" );
if (index == 0 )
return false ;
while (index != - 1 ) {
if (str.charAt(index - 1 ) != 'a' )
return false ;
if (index + 1 < n && str.charAt(index + 1 ) != 'b' )
return false ;
if (index + 2 < n && str.charAt(index + 2 ) == 'b' )
return false ;
if (index == n - 1 )
return false ;
index = str.indexOf( "b" , index + 2 );
}
return true ;
}
public static void main(String[] args)
{
String str = "abbaaabbabba" ;
int n = str.length();
System.out.println(isValidString(str, n));
}
}
|
Python 3
def isValidString( str , n):
idx = str .find( "b" )
if (idx = = 0 ):
return False
while (idx ! = - 1 ):
if ( str [idx - 1 ] ! = 'a' ):
return False
if (idx + 1 < n and str [idx + 1 ] ! = 'b' ):
return False
if (idx + 2 < n and str [idx + 2 ] = = 'b' ):
return False
if (idx = = n - 1 ):
return False
idx = str .find( "b" , idx + 2 )
return True
if __name__ = = "__main__" :
str = "abbaaabbabba"
n = len ( str )
print (isValidString( str , n))
|
C#
using System;
class GFG
{
private static bool isValidString( string str, int n)
{
int index = str.IndexOf( "b" );
if (index == 0)
return false ;
while (index != -1)
{
if (str[index - 1] != 'a' )
return false ;
if (index + 1 < n && str[index + 1] != 'b' )
return false ;
if (index + 2 < n && str[index + 2] == 'b' )
return false ;
if (index == n - 1)
return false ;
index = str.IndexOf( "b" , index + 2);
}
return true ;
}
public static void Main()
{
string str = "abbaaabbabba" ;
int n = str.Length;
Console.WriteLine(isValidString(str, n));
}
}
|
Javascript
<script>
function isValidString(str,n)
{
let index = str.indexOf( "b" );
if (index == 0)
return false ;
while (index != -1) {
if (str[index - 1] != 'a ')
return false;
// If ' b ' is not succeeded by another ' b '
if (index + 1 < n && str[index + 1] != ' b ')
return false;
// If sub-string is of the type "abbb"
if (index + 2 < n && str[index + 2] == ' b')
return false ;
if (index == n - 1)
return false ;
index = str.indexOf( "b" , index + 2);
}
return true ;
}
let str = "abbaaabbabba" ;
let n = str.length;
document.write(isValidString(str, n));
</script>
|
Time complexity: O(n*n), the find() function takes O(n) time and in the worst case we need to do O(n) find() operations.
Auxiliary space: O(1), As constant extra space is used.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...