#include

using namespace std;

int main()

{

int t;

cin>>t;

for(int x=0;x<t;x++)

{

string s;

cin>>s;

int u=0,d=0;

for(int m=0;s[m]!=’\0’;m++)

{

if(s[m]==‘U’)

u++;

else

d++;

}

int count=0;

if(u>d)

{

for(int i=0;s[i]!=’\0’

{

if(s[i]==‘D’)

{

while(s[i]==‘D’)

i++;

count++;

i++;

}

else

i++;

}

}

else

{

for(int j=0;s[j]!=’\0’

{

if(s[j]==‘U’)

{

while(s[j]==‘U’)

j++;

count++;

j++;

}

else

j++;

}

}

cout<<count<<endl;

}

return 0;

}

unable to find bug in this code as it runs for all the test cases perfectly!

simple logic try first altering up’s one(U)- say ‘a’ moves.

Now consider altering down ones(D)- say ‘b’ moves.

final answer= MIN(a,b) = (minimum of a and b)

Your code will fail for equal no. of (U’s,D’s) and alternate arrangement

A very simple approach is to just count the number of contiguous Us and Ds. Ans is minimum of (number of contiguous Us, number of contiguous Ds).

Below is my code: https://www.codechef.com/viewsolution/14279267