Given a string **str**, the task is to check whether every group of consecutive **a’s** is followed by a group of consecutive **b’s** of 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++

`// 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 string ` `int` `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 code ` `int` `main() ` `{ ` ` ` `string s = ` `"bb"` `; ` ` ` `if` `(matchPattern(s) == ` `true` `) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// 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 string ` `static` `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 code ` `public` `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 ` `} ` |

*chevron_right*

*filter_none*

## C#

`// 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 string ` `static` `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 code ` `public` `static` `void` `Main() ` `{ ` ` ` `string` `s = ` `"bb"` `; ` ` ` `if` `(matchPattern(s) == ` `true` `) ` ` ` `Console.Write(` `"Yes"` `); ` ` ` `else` ` ` `Console.Write(` `"No"` `); ` `} ` ` ` `} ` |

*chevron_right*

*filter_none*

**Output:**

No

## Recommended Posts:

- Group Shifted String
- Group words with same set of characters
- Group all occurrences of characters according to first appearance
- Minimum swaps to group similar characters side by side?
- Check if all the palindromic sub-strings are of odd length
- Check if a binary string contains all permutations of length k
- Check length of a string is equal to the number appended at its last
- Program to find Length of Bridge using Speed and Length of Train
- Check if a string contains a palindromic sub-string of even length
- Nth Even length Palindrome
- Run Length Encoding
- Sub-strings of length K containing same character
- Sum of first K even-length Palindrome numbers
- Run Length Encoding in Python
- Repeated subsequence of length 2 or more

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.