Magical Planks

 Ryan is a boy from, who has been given a task by his father. He has 

N wooden planks, numbered from 1 to N, which are colored either black or white.

His task is to color all planks the same color! But there is some magic in the winds of his small town. Whenever he colors the ith ( plank which has the color Si ) to a color P then following events happen:

  • if 2iN and Si=Si1, then color of (i1)th plank changes to P.
  • if 1iN1 and Si=Si+1, then color of (i+1)th plank changes to P.

Now this process continues for the newly colored planks also. If none of the neighbors have same color, then nothing happens to the neighbors.

Suppose Ryan has planks which have their coloring : BBWWWB If Ryan colors the fourth plank( whose color is W ) to color B, then the finally the planks would be colored as following:

BBBBBB

Ryan can choose any one of the N planks and change its color as many times as he wants. Determine the minimum number of times Ryan has to paint a plank such that all planks get the same color at the end.

Input Format

  • First line will contain T, number of testcases. Then the testcases follow.
  • The first line of each test case consists of an integer N the number of planks
  • Second line of each test case consists of a string S of size N,where the i th character denotes the color of plank i

Output Format

For each testcase, output a single integer denoting the minimum number of times Ryan has to paint a single plank such that all planks get the same color at the end.

Constraints

  • 1T105
  • 1N105
  • S consists only of characters B and W
  • The sum of N over all cases doesn't exceed 105.

Sample Input 1 

4
6
BBWWWB
5
WWBWB
2
BB
9
WWBBBBBWW

Sample Output 1 

1
2
0
1

Explanation

Test case 1: The first test case is already described in the question.

Test case 2: Ryan can paint the third plank to W. After doing so the color string of planks become WWWWB. Then he can again paint the third plank to the color B. After doing so the string goes through following transitions:

  • The color of third plank changes to B. ( The string becomes WWBWB )
  • The color of second and fourth plank changes to B. ( The string becomes WBBBB )
  • The color of first plank changes to B. ( The string becomes BBBBB )

Finally, all planks have same color.

Test case 3: All planks have the same color.

Test case 4: Ryan can paint any of the planks numbered from 3 to 7 to W, and all these planks will be colored to W!

Code:

#include <bits/stdc++.h>

using namespace std;


int main() {

// your code goes here

int t,n;

cin>>t;

while(t--){

    cin>>n;

    string s; cin>>s;int temp = 0;

    int answer =0;

    for(int i=0;i<n;i++){

        if(s[i] == 'W'){

            int j=i+1;

            while(j<n && s[j] == s[i]) j++;

            temp +=j;

        }

        else if(i>0 && s[i] != s[i-1]){

            answer++;

            int k = i + 1;

            while(k<n && s[k] == s[i]) k++;

            temp+=k;

        }

        else{

            int k = i + 1;

            while(k<n && s[k] == s[i]) k++;

            temp+=k;

        }

    }

    if(temp) cout<<answer<<endl;

    else cout<<0<<endl;

}

}


Comments

Post a Comment

Popular posts from this blog

Flipkart Runway: Season 3 | 2025 | Internship

Walmart Sparkplug 2022 | 150 students will be selected for summer internship | ₹1-1.1 lakh monthly

Top-order failure leaves India in the doldrums

Software Testing Week 5 : Assignment 5 | 2022