Check If every group of a’s is followed by a group of b’s of same length
Last Updated :
12 Sep, 2022
Given string str, the task is to check whether every group of consecutive a’s is followed by a group of consecutive b’s of the same length. If the condition is true for every group then print 1 else print 0.
Examples:
Input: str = “ababaabb”
Output: 1
ab, ab, aabb. All groups are valid
Input: str = “aabbabb”
Output: 0
aabb, abb (A single ‘a’ followed by 2 ‘b’)
Approach:
- For every a in the string increment the count.
- Starting from the first b, decrement the count for every b.
- If at the end of the above cycle, count != 0 then return false.
- Else repeat the first two steps for the rest of the string.
- Return true if the condition is satisfied for all the cycles else print 0.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int matchPattern(string s)
{
int count = 0;
int n = s.length();
int i = 0;
while (i < n) {
while (i < n && s[i] == 'a' ) {
count++;
i++;
}
while (i < n && s[i] == 'b' ) {
count--;
i++;
}
if (count != 0)
return false ;
}
return true ;
}
int main()
{
string s = "bb" ;
if (matchPattern(s) == true )
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
public class GFG{
static boolean matchPattern(String s)
{
int count = 0 ;
int n = s.length();
int i = 0 ;
while (i < n) {
while (i < n && s.charAt(i) == 'a' ) {
count++;
i++;
}
while (i < n && s.charAt(i) == 'b' ) {
count--;
i++;
}
if (count != 0 )
return false ;
}
return true ;
}
public static void main(String []args)
{
String s = "bb" ;
if (matchPattern(s) == true )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def matchPattern(s):
count = 0 ;
n = len (s);
i = 0 ;
while (i < n) :
while (i < n and s[i] = = 'a' ):
count + = 1 ;
i = + 1 ;
while (i < n and s[i] = = 'b' ):
count - = 1 ;
i + = 1 ;
if (count ! = 0 ):
return False ;
return True ;
s = "bb" ;
if (matchPattern(s) = = True ):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
public class GFG{
static bool matchPattern( string s)
{
int count = 0;
int n = s.Length;
int i = 0;
while (i < n) {
while (i < n && s[i] == 'a' ) {
count++;
i++;
}
while (i < n && s[i] == 'b' ) {
count--;
i++;
}
if (count != 0)
return false ;
}
return true ;
}
public static void Main()
{
string s = "bb" ;
if (matchPattern(s) == true )
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
PHP
<?php
function matchPattern( $s )
{
$count = 0;
$n = strlen ( $s );
$i = 0;
while ( $i < $n ) {
while ( $i < $n && $s [ $i ] == 'a' ) {
$count ++;
$i ++;
}
while ( $i < $n && $s [ $i ] == 'b' ) {
$count --;
$i ++;
}
if ( $count != 0)
return false;
}
return true;
}
$s = "bb" ;
if (matchPattern( $s ) == true)
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function matchPattern(s)
{
let count = 0;
let n = s.length;
let i = 0;
while (i < n)
{
while (i < n && s[i] == 'a ')
{
count++;
i++;
}
// Count b' s in current segment
while (i < n && s[i] == 'b' )
{
count--;
i++;
}
if (count != 0)
return false ;
}
return true ;
}
let s = "bb" ;
if (matchPattern(s) == true )
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Complexity Analysis:
- Time Complexity : O( | s | ) ,where | s | is length of given string s.
- Space Complexity : O(1) ,as we are not using any extra space
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...