Coloured Substring

You are given a string S of length N consisting only of the characters 'R', 'G', and 'B'. You are also given an integer K.

You want to change the minimum number of characters in S so that it contains a substring of length K which is also a substring of the infinite string "RGBRGBRGB...".

Find the minimum number of character changes required.

Example 1:

Input: n = 5, k = 2 ,s = "BGGGG"

Output: 1

Explanation :Possible valid substrings: RG, GB, BR

Minimum changes = 1

Example 2:

Input: n = 5, k = 3 ,s = "RBRGR"

Output: 0

Explanation: Substring "BRG" already matches pattern.

Now Your Turn!

Pick the correct output for the given input

Input: n = 5, k = 5 s = "BBBRR"

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 ≤ k ≤ n ≤ 2 * 105
  • Σn ≤ 2 * 105

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int minChanges(int n, int k, string& s) {
// Write your code here
return 0;
}
};
Test Case

Input:

K
N
S