


You have to insert “abc” to “inputString” in every operation.
If “inputString” is empty just insert “abc” in “inputString”
If “inputString” is not empty, you can insert “abc” in any position in “inputString” in such a way that:
1. “ LEFT PORTION OF “inputString” ” + “ abc ” + “ RIGHT PORTION OF “inputString” ” = “Beautiful String”.
2. “ LEFT PORTION OF “inputString” ” maybe EMPTY OR “a” OR “ab” OR “abc”.
3. “ LEFT PORTION OF “inputString” ” maybe EMPTY OR “c” OR “bc” OR “abc”.
The given string "inputString" cannot be empty.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.
The next line of each test case contains a string “inputString”, representing the string for which you have to determine if the string is a “Beautiful String” or not.
For each test case, print a single line as “True” or “False” as per the given condition.
The output of each test case will be printed in a separate line.
You don’t need to print anything, It has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= |inputString| <= 1000
Where “inputString” will have only lowercase alphabets and |inputString| represents the length of the input string.
Time Limit: 1 sec
The idea is to insert “abc” at every position in a string , “helperString” (initialised as an empty string) recursively and comparing it with the input string “inputString” at every recursive step. If the “helperString” comes out to be equal to input string “inputString” then return “True” to the function else “False”.
We can use a stack to store the characters of “inputString”. We will iterate over the “inputString”. If it is ‘a’, we will push it to the stack , if it is ‘b’, we need to check the top of the stack. If the stack is empty, thus it will not be a “Beautiful String”. If the top of the stack is not ‘a’, then it will also not be a “Beautiful String” as it can’t be substring “abc”. Otherwise, we will pop the element from stack and will push ‘b’ into the stack.
Similarly, if ‘c’ is encountered during the iteration, we need to check if the top of the stack is ‘b’ then we will pop the top element from the stack but we do not need to push ‘c’ back to the stack. The process continues until the string is all iterated, and at last, the stack should be empty which indicates the string “inputString” is a “Beautiful String”.
Note:
You can do this approach with the help of vector also in which during the iteration of “inputString”, if we encountered with ‘a’ or ‘b’ just simply push it into the vector and if the encountered element is ‘c’, just check if size of vector is not less than 2 or previous 2 elements in the vector should be ‘a’ and ‘b’ respectively. Then just simply pop the previous 2 elements from the vector and continue this process until full iteration. After iteration, just check if the vector is empty then return “True” to the function else “False”.
We can use two pointers, one pointer ‘iteratorPointer’ to traverse the string “inputString” and another pointer ‘searchPointer’ to find the substring “abc” in “inputString” (both pointers pointing to 0 initially).