Red OR Green | Practice | GeeksforGeeks

Given a string of length N, made up of only uppercase characters 'R' and 'G', where 'R' stands for Red and 'G' stands for Green.Find out the minimum number of characters you need to change to make the whole string of the same colour.
Example 1:
Input:
N=5
S="RGRGR"
Output:
2
Explanation:
We need to change only the 2nd and 
4th(1-index based) characters to 'R', 
so that the whole string becomes 
the same colour.
Example 2:
Input:
N=7
S="GGGGGGR"
Output:
1
Explanation:
We need to change only the last 
character to 'G' to make the string 
same-coloured.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function RedOrGreen() which takes the Integer N and the string S as input parameters and returns the minimum number of characters that have to be swapped to make the string same-coloured.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1<=N<=105
S consists only of characters 'R' and 'G'.
0
akash22bhin 9 hours
Total Time Taken:
0.31/1.5
int RedOrGreen(int N,string S) {          //code here          int cR=0,cG=0;          for(int i=0;i<N;i++)          {              if(S[i]=='R')              cR++;              else              cG++;          }          if(cG>=cR)          return cR;          else          return cG;             }
 
0
aniket6518gadhe2 days ago
JAVA SOLUTION
Total Time Taken:
0.41/1.5
 
static int RedOrGreen(int N, String S) {
        int rCount=0;
        for(Character c:S.toCharArray()){
            if(c=='R')
                rCount++;
        }
        rCount=Math.min(rCount,N-rCount);
        return rCount;
    }
0
thaloravinash101 week ago
class Solution:   def RedOrGreen(self,N,S):       r=0       g=0       for i in S:           if i=="R":               r=r+1           else:               g=g+1       return min(r,g)
0
nishraut72 weeks ago
python code:
 
class Solution:   def RedOrGreen(self,N,S):       #code here       count_R = 0       count_G = 0       finalcount = 0              for i in S:           if i == "G":               count_G += 1           else:               count_R += 1                      if count_G < count_R:           finalcount = count_G       else:           finalcount = count_R                  return finalcount
+1
sindhumaram121 month ago
class Solution:   def RedOrGreen(self,N,S):       #code here       l=[]       l.append(S.count('G'))       l.append(S.count('R'))       return min(l)
0
rahulrs2 months ago
JAVA EASY CODE.......................
 
static int RedOrGreen(int N, String S) {       //code here       int green = 0,red=0;       for(int i=0;i<N;i++){           if(S.charAt(i)=='G'){               green++;           }else{               red++;           }       }       int change = Math.min(green, red);       return change;   }
0
akhilkhan0786khan2 months ago
js code
 
class Solution{   RedOrGreen(n, str){       //code here       var Rcount = 0;       var Gcount = 0;       var sub = n;       for(var i = 0; i<n; i++)       {           if(str[i]=="R")           {               Rcount++;           }else           {               Gcount++;           }       }       if(Rcount>Gcount)       {           var x = sub-Rcount;           return x;       }else       {           var y = sub-Gcount;           return y;       }   }}
0
tejaschavan81712 months ago
Cpp code
 int count1=0;            int count2=0;                    for(int i=0;i<S.size();i++)          {              if(S[i]=='G')              {                  count1++;              }else if(S[i]=='R')          {              count2++;          }                        }          if(count1>count2)          {              return count2;          }else          return count1;        
0
vikkrammor2 months ago
static int RedOrGreen(int N, String S) {
        int red = 0;
           int green = 0;
           int n = S.length();
           for(int i=0; i<n; i++){
               if(S.charAt(i) == 'R'){
                   red++;
               }
               else{
                   green++;
               }
           }
           if(red >= green){
               return green;
           }
           else{
               return red;
           }
    }
0
vikkrammor
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.