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.
Examples:
Input: str = “abbaaabbabba”
Output: 1Input: str = “abbaaababb”
Output: -1
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:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns 1 if str is valid bool isValidString(string str, int n) { // Index of first appearance of 'b' int index = find(str.begin(), str.end(), 'b') - str.begin(); // If str starts with 'b' if (index == 0) return false; // While 'b' occurs in str while (index <= n - 1) { // If 'b' doesn't appear after an 'a' if (str[index - 1] != 'a') return false; // If 'b' is not succeeded by another 'b' if (index + 1 < n && str[index + 1] != 'b') return false; // If sub-string is of the type "abbb" if (index + 2 < n && str[index + 2] == 'b') return false; // If str ends with a single b if (index == n - 1) return false; index = find(str.begin() + index + 2, str.end(), 'b') - str.begin(); } return true; } // Driver code int main() { string str = "abbaaabbabba"; int n = str.length(); isValidString(str, n) ? cout << "true" : cout << "false"; return 0; } // This code is contributed by // sanjeev2552 |
Java
// Java implementation of the approach class GFG { // Function that returns 1 if str is valid private static boolean isValidString(String str, int n) { // Index of first appearance of 'b' int index = str.indexOf("b"); // If str starts with 'b' if (index == 0) return false; // While 'b' occurs in str while (index != -1) { // If 'b' doesn't appear after an 'a' if (str.charAt(index - 1) != 'a') return false; // If 'b' is not succeeded by another 'b' if (index + 1 < n && str.charAt(index + 1) != 'b') return false; // If sub-string is of the type "abbb" if (index + 2 < n && str.charAt(index + 2) == 'b') return false; // If str ends with a single b if (index == n - 1) return false; index = str.indexOf("b", index + 2); } return true; } // Driver code public static void main(String[] args) { String str = "abbaaabbabba"; int n = str.length(); System.out.println(isValidString(str, n)); } } |
Python 3
# Python 3 implementation of the approach # Function that returns 1 if str is valid def isValidString(str, n): # Index of first appearance of 'b' idx = str.find("b") # If str starts with 'b' if (idx == 0): return False # While 'b' occurs in str while (idx != -1): # If 'b' doesn't appear after an 'a' if (str[idx - 1] != 'a'): return False # If 'b' is not succeeded by another 'b' if (idx + 1 < n and str[idx + 1] != 'b'): return False # If sub-string is of the type "abbb" if (idx + 2 < n and str[idx + 2] == 'b'): return False # If str ends with a single b if (idx == n - 1): return False idx = str.find("b", idx + 2) return True # Driver code if __name__ == "__main__": str = "abbaaabbabba" n = len(str) print(isValidString(str, n)) # This code is contributed by ita_c |
C#
// C# implementation of the approach using System; class GFG { // Function that returns 1 if str is valid private static bool isValidString(string str, int n) { // Index of first appearance of 'b' int index = str.IndexOf("b"); // If str starts with 'b' if (index == 0) return false; // While 'b' occurs in str while (index != -1) { // If 'b' doesn't appear after an 'a' if (str[index - 1] != 'a') return false; // If 'b' is not succeeded by another 'b' if (index + 1 < n && str[index + 1] != 'b') return false; // If sub-string is of the type "abbb" if (index + 2 < n && str[index + 2] == 'b') return false; // If str ends with a single b if (index == n - 1) return false; index = str.IndexOf("b", index + 2); } return true; } // Driver code public static void Main() { string str = "abbaaabbabba"; int n = str.Length; Console.WriteLine(isValidString(str, n)); } } // This code is contributed by Ryuga |
true
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.
Recommended Posts:
- Check if a string has m consecutive 1's or 0's
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Count occurrences of a sub-string with one variable character
- Length of longest consecutive ones by at most one swap in a Binary String
- Count possible binary strings of length N without P consecutive 0s and Q consecutive 1s
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Check if all substrings of length K of a Binary String has equal count of 0s and 1s
- Check if a binary string has a 0 between 1s or not | Set 1 (General approach)
- 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
- Maximum consecutive one’s (or zeros) in a binary array
- Maximum consecutive one’s (or zeros) in a binary circular array
- Order of teams in a tournament such that every team has won against its consecutive team
- Check if a binary string contains consecutive same or not
- Count occurrences of a string that can be constructed from another given string
- NFA to accept strings that has atleast one character occurring in a multiple of 3
- Occurrences of a pattern in binary representation of a number
- Construct a string that has exactly K subsequences from given string
- Python | Check if there are K consecutive 1's in a binary number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

