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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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 string follows order of characters defined by a pattern or not | Set 1
- Count of occurrences of a "1(0+)1" pattern in a string
- Program for Reversed String Pattern
- Printing string in plus ‘+’ pattern in the matrix
- Python | Get the smallest window in a string containing all characters of given pattern
- Program to print a doormat pattern having a string written in the center in Python
- 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 be formed from another string by at most X circular clockwise shifts
- Check if a string can become empty by recursively deleting a given sub-string
- Check if left and right shift of any string results into given string
- Check if any permutation of string is a K times repeated string
- Check if a string can be obtained by rotating another string d places
- Queries to check if string B exists as substring in string A
- Check if a string can be obtained by rotating another string 2 places
- Check if a string can be repeated to make another string
- Python | Check if a given string is binary string or not
- Check if a string is a scrambled form of another string