
0011 , 1100 , 101010 etc are beautiful strings whereas 1110, 0001,10101 etc are not beautiful strings.
Let the given string be “101001”
We will return 3 as we can divide the string into 3 beautiful strings “10” “10” and “01’.
The first line of input contains an integer 'T’ denoting the number of test cases to run. Then the test cases follow.
The first line of each test case contains the string ‘str’.
For each test case, return the maximum number of substrings, ‘str’ can be split into such that each substring is beautiful.
If there are none, return -1.
Output for each test case will be printed in a new line.
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 5000
Time limit: 1 second
The key idea in solving this problem is to simply iterate through the given string and keep the count of 1s and 0s in the string. Whenever we have the count of 1 and 0 to be equal, we increment the count of beautiful strings by 1. If we find no beautiful string, we simply return -1.
The algorithm will be -