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.
Input : str = "aabb" Output : Yes Input : str = "abab" Output : Yes Input : str = "aabbb" Output : No
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 :
This article is contributed by Aditya Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Check if string follows order of characters defined by a pattern or not | Set 1
- Check if string follows order of characters defined by a pattern or not | Set 2
- Check if string follows order of characters defined by a pattern or not | Set 3
- Check if given Binary string follows then given condition or not
- Python | Check if a given string is binary string or not
- Check if a given string is a Reverse Bitonic String or not
- Python program to check if a string is palindrome or not
- Check if a binary string has a 0 between 1s or not | Set 1 (General approach)
- Check if a string is Isogram or not
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Check if any anagram of a string is palindrome or not
- Check whether a given string is Heterogram or not
- Check if two same sub-sequences exist in a string or not
- Check if there exists any sub-sequence in a string which is not palindrome
- Check whether the vowels in a string are in alphabetical order or not
- Check whether count of distinct characters in a string is Prime or not
- Check whether the frequencies of all the characters in a string are prime or not
- Check if a binary string contains consecutive same or not
- Check whether the Average Character of the String is present or not
- Check if all the 1's in a binary string are equidistant or not