Algorithm


C. Hamsters and Tigers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Today there is going to be an unusual performance at the circus — hamsters and tigers will perform together! All of them stand in circle along the arena edge and now the trainer faces a difficult task: he wants to swap the animals' positions so that all the hamsters stood together and all the tigers also stood together. The trainer swaps the animals in pairs not to create a mess. He orders two animals to step out of the circle and swap places. As hamsters feel highly uncomfortable when tigers are nearby as well as tigers get nervous when there's so much potential prey around (consisting not only of hamsters but also of yummier spectators), the trainer wants to spend as little time as possible moving the animals, i.e. he wants to achieve it with the minimal number of swaps. Your task is to help him.

Input

The first line contains number n (2 ≤ n ≤ 1000) which indicates the total number of animals in the arena. The second line contains the description of the animals' positions. The line consists of n symbols "H" and "T". The "H"s correspond to hamsters and the "T"s correspond to tigers. It is guaranteed that at least one hamster and one tiger are present on the arena. The animals are given in the order in which they are located circle-wise, in addition, the last animal stands near the first one.

Output

Print the single number which is the minimal number of swaps that let the trainer to achieve his goal.

Examples
input
Copy
3
HTH
output
Copy
0
input
Copy
9
HTHTHTHHT
output
Copy
2
Note

In the first example we shouldn't move anybody because the animals of each species already stand apart from the other species. In the second example you may swap, for example, the tiger in position 2 with the hamster in position 5 and then — the tiger in position 9 with the hamster in position 7.



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <iostream>

int main(){

    long n; std::cin >> n;
    std::string s; std::cin >> s;

    long m(0);
    for(long p = 0; p < n; p++){m += (s[p] == 'H');}
    long minCount(n + 1);
    for(long start = 0; start < n; start++){
        long cnt(0);
        for(long p = 0; p < m; p++){cnt += (s[(start + p) % n] == 'T');}
        minCount = (minCount < cnt) ? minCount : cnt;
    }

    printf("%ld\n", minCount);

    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3
HTH
Advertisements

Demonstration


Codeforces Solution-C. Hamsters and Tigers-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+