


Suppose we are given an empty string “inputString”. We have to convert this “inputString” into a “Beautiful String”
We can perform any number of operations on “inputString” to convert it to “Beautiful String”.
Operation :
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”.
Your task is to find whether the given string “inputString” is a “Beautiful String”, or not.
Note :The given string "inputString" cannot be empty.
Input Format :
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.
Output Format :
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.
Note :
You don’t need to print anything, It has already been taken care of. Just implement the given function.
Constraints :
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
2
abcabc
abccba
True
False
Test Case 1 :
For the first test case, the given string is “abcabc” so we return “True” as first we add “abc” then again “abc” so we can say we are able to build the string “inputString” to “Beautiful String”.
Test Case 2 :
For this test case, the given string is “abccba” so we return “False” as first we add “abc” then we can’t add “abc” as “cba” is present so we return false.
2
aabcbc
babcc
True
False
Can you think of a Brute Force approach?
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”.
Algorithm:
O(N ^ N), where ‘N’ is the length of the input string.
As we are calling the recursive function for every possible position in the generated string, so overall time complexity becomes equal to O(N ^ N).
O(N), where ‘N’ is the length of the input string.
Since we are using an additional string that will be of maximum length equal to that of the input string, the overall space complexity is O(N).