Given a string str consisting of only characters ‘a’ and ‘b’, the task is to check whether the string is valid or not. In a valid string, every group of consecutive b must be of length 2 and must appear after 1 or more occurrences of character ‘a’ i.e. “abba” is a valid sub-string but “abbb” and aba are not. Print 1 if the string is valid else print -1.
Input: str = “abbaaabbabba”
Input: str = “abbaaababb”
Approach: Find every occurrence of ‘b’ in the string and check whether it is a part of the sub-string “abb”. If the condition fails for any sub-string then print -1 else print 1.
Below is the implementation of the above approach:
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 a binary string contains consecutive same or not
- Rearrange a binary string as alternate x and y occurrences
- Split a binary string into K subsets minimizing sum of products of occurrences of 0 and 1
- Python | Check if there are K consecutive 1's in a binary number
- Check if a string has m consecutive 1's or 0's
- Maximum Consecutive Zeroes in Concatenated Binary String
- Length of longest consecutive ones by at most one swap in a Binary String
- Binary string with given frequencies of sums of consecutive pairs of characters
- Check if binary representations of 0 to N are present as substrings in given binary string
- Python | Check if a given string is binary string or not
- Check divisibility of binary string by 2^k
- Check if all the 1's in a binary string are equidistant or not
- Check if given Binary string follows then given condition or not
- Check if a binary string has a 0 between 1s or not | Set 1 (General approach)
- Check if it is possible to rearrange a binary string with alternate 0s and 1s
- Check if a binary string contains all permutations of length k
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Check if an encoding represents a unique binary string
- Check whether a binary string can be formed by concatenating given N numbers sequentially
- Count occurrences of a string that can be constructed from another given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.