# Check if a string follows a^nb^n pattern or not

Given a string str, return true string follows pattern anbn, i.e., it has a’s followed by b’s such that the number of a’s and b’s are same.

Examples:

```Input : str = "aabb"
Output : Yes

Input : str = "abab"
Output : Yes

Input : str = "aabbb"
Output : No
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is to first count a’s. If number of a’s is not equal to half of string’s length, then return false. Else check if all remaining characters are b’s or not.

Below is the implementation of above idea :

## C/C++

 `// C++ program to check if a string is of ` `// the form a^nb^n. ` `#include ` `using` `namespace` `std; ` ` `  `// Returns true str is of the form a^nb^n. ` `bool` `isAnBn(string str) ` `{ ` `   ``int` `n = str.length(); ` ` `  `   ``// After this loop 'i' has count of a's ` `   ``int` `i; ` `   ``for` `(i=0; i

## Java

 `// Java program to check if a string is of ` `// the form a^nb^n. ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` ` `  `class` `CheckPattern ` `{ ` `    ``public` `static` `boolean` `isAnBn(String s) ` `    ``{ ` `        ``int` `l = s.length(); ` `         `  `        ``// Only even length strings will have same number of a's and b's ` `        ``if` `(l%``2` `== ``1``) ` `        ``{ ` `            ``return` `false``; ` `        ``} ` `        ``// Set two pointers, one from the left and another from right ` `        ``int` `i = ``0``; ` `        ``int` `j = l-``1``; ` `         `  `        ``// Compare the characters till the center ` `        ``while` `(i

## Python 3

 `# Python 3program to check if a  ` `# string is of the form a^nb^n. ` ` `  `# Returns true str is of the  ` `# form a^nb^n. ` `def` `isAnBn(``str``): ` `     `  `    ``n ``=` `len``(``str``) ` `     `  `    ``# After this loop 'i' has  ` `    ``# count of a's ` `    ``for` `i ``in` `range``(n): ` `        ``if` `(``str``[i] !``=` `'a'``): ` `            ``break` `     `  `    ``# Since counts of a's and b's should ` `    ``# be equal, a should apear exactly ` `    ``# n/2 times ` `    ``if` `(i ``*` `2` `!``=` `n): ` `        ``return` `False` `     `  `    ``# Rest of the characters must ` `    ``# be all 'b' ` `    ``for` `j ``in` `range``(i, n): ` `        ``if` `(``str``[j] !``=` `'b'``): ` `            ``return` `False` `     `  `    ``return` `True` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"``: ` `    ``str` `=` `"abab"` `    ``print``(``"Yes"``) ``if` `isAnBn(``str``) ``else` `print``(``"No"``) ` ` `  `# This code is contributed  ` `# by ChitraNayal `

## C#

 `// C# program to check if a string  ` `// is of the form a^ nb ^ n. ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Function returns true str is of the form a^nb^n. ` `    ``public` `static` `bool` `isAnBn(String s) ` `    ``{ ` `        ``int` `l = s.Length; ` `         `  `        ``// Only even length strings will have ` `        ``// same number of a's and b's ` `        ``if` `(l % 2 == 1) ` `        ``{ ` `            ``return` `false``; ` `        ``} ` `         `  `        ``// Set two pointers, one from the ` `        ``// left and another from right ` `        ``int` `i = 0; ` `        ``int` `j = l - 1; ` `         `  `        ``// Compare the characters  ` `        ``// till the center ` `        ``while` `(i < j) ` `        ``{ ` `            ``if``(s[i] != ``'a'` `|| s[j] != ``'b'``) ` `            ``{ ` `                ``return` `false``; ` `            ``} ` `            ``i++; ` `            ``j--; ` `        ``} ` `        ``return` `true``;  ` `    ``} ` `     `  `    ``// Driver Code ` `    ``public` `static` `void` `Main ()  ` `    ``{ ` `        ``String s = ``"abab"``; ` `         `  `        ``// Function call ` `        ``bool` `value = isAnBn(s); ` `        ``if``(value == ``true``) ` `        ``{ ` `            ``Console.Write(``"Yes"``); ` `        ``} ` `        ``else` `        ``{ ` `            ``Console.Write(``"No"``); ` `        ``} ` `    ``} ` `} ` ` `  `// This code is contributed by Nitin Mittal. `

## PHP

 ` `

Output:

```No
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.