Garlanding in the FIFA Mars Cup
Problem Statement:
In order to commemorate the opening ceremony of the FIFA Mars Cup, the organisers decide to give every player a garland of red and green flowers. The organisers of the FIFA Mars Cup want to make sure that all the garlands that they give to the players are beautiful. There are T garlands each consisting of N flowers placed in a cyclic order. Since Martians believe in change, a garland is considered beautiful if no two adjacent flowers are of the same color. To make the garland beautiful you may perform the following operation at most K times:
- Make two cuts on the rope (not intersecting the flowers) to split the garland
- Reverse on of the split parts
- Join the parts again end-to-end
For each garland your task is to find whether you can make a beautiful garland.
Constraints:
- 1 ≤ T ≤ 100
- 1 ≤ N ≤ 103
Subtask 1: 50 points
- K = 1000000000
Subtask 2: 50 points
- K = 1
Input Format:
- The first line contains a single integer T
- The next T testcases contain two lines of input
- The first line contains two integers N and K
- The second line contains a string S of length N consisting of the characters ‘R’ and ‘G’
Output Format:
- For each testcase display “YES” if it is possible to make a beautiful garland and “NO” if it is impossible to make a beautiful garland from the current garland
Sample input:
3
2 1000000000
RG
4 1
RRGG
2 1
RR
Sample output:
YES
YES
NO
Solution:
# Loop through all testcases
for _ in range(int(input())):
# Take input and print sum
n, k = map(int, input().split())
s = input()
r = s.count('R')
g = s.count('G')
if r != g:
print('NO')
continue
if k == int(1e9):
print('YES')
continue
a = 0
b = 0
for i in range(n - 1):
if s[i] == s[i + 1]:
if s[i] == 'R':
a += 1
else:
b += 1
if a == 1 and b == 1:
print('YES')
else:
print('NO')