
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Check if a string follows anbn pattern or not in C++
Suppose, we are given a string that is made of only two letters a and b. We have to find out if the string is of the form anbn, or in other words it contains n number of a's followed by n number of b's. If true, we return 1 otherwise 0.
So, if the input is like "aaaaaaaaaaaabbbbbbbbbbbb", then the output will be true.
To solve this, we will follow these steps −
- length := length of input_string
- for initialize i := 0, when i < length, update (increase i by 1), do &minus
- if input_string[i] is not equal to 'a', then −
- Come out from the loop
- if input_string[i] is not equal to 'a', then −
- if i * 2 is not equal to length, then −
- return false
- for initialize j := i, when j < length, update (increase j by 1), do −
- if input_string[j] is not equal to 'b', then −
- return false
- if input_string[j] is not equal to 'b', then −
- return true
Example (C++)
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; bool solve(string input_string) { int length = input_string.length(); int i; for (i = 0; i < length; i++) if (input_string[i] != 'a') break; if (i * 2 != length) return false; for (int j = i; j < length; j++) if (input_string[j] != 'b') return false; return true; } int main() { string input_string = "aaaaaaaaaaaabbbbbbbbbbbb"; cout << solve(input_string)<< endl; return 0; }
Input
"aaaaaaaaaaaabbbbbbbbbbbb"
Output
1
- Related Articles
- Check if a string follows a^n b^n pattern or not in Python
- Check if string follows order of characters defined by a pattern or not in Python
- Check if a string is Isogram or not in Python
- Python - Check if a given string is binary string or not
- Program to check regular expression pattern is matching with string or not in Python
- Check if a binary string contains consecutive same or not in C++
- Python program to check if a string is palindrome or not
- Java Program to check if a string is empty or not
- C# program to check if a string is palindrome or not
- Check if any anagram of a string is palindrome or not in Python
- C# program to check if string is panagram or not
- Swift program to check if string is pangram or not
- C program to check if a given string is Keyword or not?
- Python program to check if a given string is Keyword or not
- Swift Program to check if a given string is Keyword or not

Advertisements