Check If every group of a's is followed by a group of b's of same length - GeeksforGeeks

10 May, 2021
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 validInput: 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++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to match whether there are always n consecutive b's// followed by n consecutive a's throughout the stringint matchPattern(string s){    int count = 0;    int n = s.length();     // Traverse through the string    int i = 0;    while (i < n) {         // Count a's in current segment        while (i < n && s[i] == 'a') {            count++;            i++;        }         // Count b's in current segment        while (i < n && s[i] == 'b') {            count--;            i++;        }         // If both counts are not same.        if (count != 0)            return false;    }     return true;} // Driver codeint main(){    string s = "bb";    if (matchPattern(s) == true)        cout << "Yes";    else        cout << "No";    return 0;}
// Java implementation of the above approach public class GFG{ // Function to match whether there are always n consecutive b's// followed by n consecutive a's throughout the stringstatic boolean matchPattern(String s){    int count = 0;    int n = s.length();     // Traverse through the string    int i = 0;    while (i < n) {         // Count a's in current segment        while (i < n && s.charAt(i) == 'a') {            count++;            i++;        }         // Count b's in current segment        while (i < n && s.charAt(i) == 'b') {            count--;            i++;        }         // If both counts are not same.        if (count != 0)            return false;    }     return true;} // Driver codepublic static void main(String []args){    String s = "bb";    if (matchPattern(s) == true)        System.out.println("Yes");    else        System.out.println("No");} // This code is contributed by Ryuga}
# Python 3 implementation of the approach # Function to match whether there are# always n consecutive b's followed by# n consecutive a's throughout the stringdef matchPattern(s):     count = 0;    n = len(s);     # Traverse through the string    i = 0;    while (i < n) :         # Count a's in current segment        while (i < n and s[i] == 'a'):             count += 1 ;            i =+ 1;         # Count b's in current segment        while (i < n and s[i] == 'b'):            count -= 1 ;            i += 1;             # If both counts are not same.        if (count != 0):            return False;     return True; # Driver codes = "bb";if (matchPattern(s) == True):    print("Yes");else:    print("No"); # This code is contributed# by Akanksha Rai
// C# implementation of the above approach  using System;public class GFG{  // Function to match whether there are always n consecutive b's// followed by n consecutive a's throughout the stringstatic bool matchPattern(string s){    int count = 0;    int n = s.Length;      // Traverse through the string    int i = 0;    while (i < n) {          // Count a's in current segment        while (i < n && s[i] == 'a') {            count++;            i++;        }          // Count b's in current segment        while (i < n && s[i] == 'b') {            count--;            i++;        }          // If both counts are not same.        if (count != 0)            return false;    }      return true;}  // Driver codepublic static void Main(){    string s = "bb";    if (matchPattern(s) == true)        Console.Write("Yes");    else        Console.Write("No");} }
<?php//PHP implementation of the approach // Function to match whether there are always n consecutive b's// followed by n consecutive a's throughout the stringfunction  matchPattern($s){    $count = 0;    $n = strlen($s);     // Traverse through the string    $i = 0;    while ($i < $n) {         // Count a's in current segment        while ($i < $n && $s[$i] == 'a') {            $count++;            $i++;        }         // Count b's in current segment        while ($i < $n && $s[$i] == 'b') {            $count--;            $i++;        }         // If both counts are not same.        if ($count != 0)            return false;    }     return true;} // Driver code      $s = "bb";    if (matchPattern($s) == true)        echo  "Yes";    else        echo "No";      // This code is contributed by ajit?>
<script>     // Javascript implementation of    // the above approach         // Function to match whether there are    // always n consecutive b's    // followed by n consecutive a's    // throughout the string    function matchPattern(s)    {        let count = 0;        let n = s.length;         // Traverse through the string        let i = 0;        while (i < n)        {             // Count a's in current segment            while (i < n && s[i] == 'a')            {                count++;                i++;            }             // Count b's in current segment            while (i < n && s[i] == 'b')            {                count--;                i++;            }             // If both counts are not same.            if (count != 0)                return false;        }         return true;    }         let s = "bb";    if (matchPattern(s) == true)        document.write("Yes");    else        document.write("No");     </script>
No
 
ankthon
ukasp
jit_t
Akanksha_Rai
mukesh07
array-traversal-question
binary-string
C++ Programs
Competitive Programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Passing a function as a parameter in C++
Program to implement Singly Linked List in C++ using class
Const keyword in C++
cout in C++
Dynamic _Cast in C++
Competitive Programming - A Complete Guide
Practice for cracking any coding interview
Arrow operator -> in C/C++ with Examples
Prefix Sum Array - Implementation and Applications in Competitive Programming
Fast I/O for Competitive Programming