Self-Destructing Strings

here is the question,
https://www.codechef.com/COOK125B/problems/SDSTRING
and here is my solution
#include <bits/stdc++.h>
using namespace std;
#define boost ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);

signed main()
{
boost;

int t=0;
cin>>t;
while(t--)
{
   string s;
   cin>>s;
   if(s.size()%2==1)
   cout<<-1<<endl;
   else
   {
       int c=0;
       deque<char> v;
       v.push_front(s[0]);
       for(int i=1;s[i];i++)
       {
           if(s[i]!=v.front()&&!v.empty())
           {
               v.pop_front();
           }
           else
           {
               v.push_front(s[i]);
           }
       } 
        if(v.size()%2==1)
        cout<<-1<<endl;
        else
        {
            
           cout<<v.size()/2<<endl;
          
            
            
        }
        
         

       
   }
    
    
}

return 0;
}
can some one please suggest me some test case in which it fails or why its wrong.

Can someone help me understand Self-Destructing Strings SDSTRING, was it asking to perform operations on the original string or that could be applied to a new string formed by the new operation?

Example:
AC code: 1000000001 ans —> 3
My code: 1000000001 —> -1 (because string could become 1010000101only if applied to original string)

It could be applied to the new string.

1
1111

Correct ans: -1
Your output: 2
Any string containing all 1’s or all 0’s should have answer -1 since there is no 0’s or 1’s to flip.

3 Likes

Give ur submission as link .Dont paste the code in the description

0000
It should return -1.

https://www.codechef.com/viewsolution/40636621 here is my solution can someone help me debug this please

sure,I will take care of it from next time.

Error Page | CodeChef check this
The approach I used:

  1. if the string is of odd length or the string has only 1 unique character the print -1.
    2)Else Make a queue, and append every element of a string in queue if the queue[0]==string[i], else if they are different then pop the leftist element from queue.
  2. with this you can get a queue with only unique elements or no elements. Now again check if the queue has odd elements then print(-1) and if have even the print(len(queue))//2 because from queue we remove the elements that can be toggle hence string can be self-destructive if we toggle those elements.

This works for me and I got the correct answer but can someone helps me to understand the case when the given string is: 111110