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 : No 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.
- 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
- Program for Reversed String Pattern
- Printing string in plus ‘+’ pattern in the matrix
- Count of occurrences of a "1(0+)1" pattern in a string
- Python | Get the smallest window in a string containing all characters of given pattern
- String slicing in Python to check if a string can become empty by recursive deletion
- Check if a string can be converted to another string by replacing vowels and consonants
- Check if a string can become empty by recursively deleting a given sub-string
- Check if a string can be obtained by rotating another string 2 places
- Queries to check if string B exists as substring in string A
- Check if a string can be obtained by rotating another string d places
- Check whether second string can be formed from characters of first string
- Check if a string contains a palindromic sub-string of even length