Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
719 B
6
Indexable
Never
def minSwaps(s):
    n = len(s)
    # GBGBGB... type of arrangement
    gb = 0
    for i in range(n):
        if(i % 2 == 0 and s[i] != 'G'):
            gb += 1
        elif(i % 2 == 1 and s[i] != 'B'):
            gb += 1

    # BGBGBG... type of arrangement
    bg = 0
    for i in range(n):
        if(i % 2 == 0 and s[i] != 'B'):
            bg += 1
        elif(i % 2 == 1 and s[i] != 'G'):
            bg += 1

    if(n % 2 == 0):  # even number of elements
        a = min(bg, gb)
        return a//2+a % 2
    else:  # odd number of elements
        if (s.count('G') > s.count('B')):  # Girls > Boys
            return gb//2+gb % 2
        else:
            return bg//2+bg % 2


s = input()
print(minSwaps(s))