-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChanges_in_a_string.cpp
More file actions
65 lines (62 loc) · 1.37 KB
/
Changes_in_a_string.cpp
File metadata and controls
65 lines (62 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* @Date : 2020-04-24 07:27:48
* @Author : Abhimanyu Kumar Maurya (aerma7309@gmail.com)
*/
#include <bits/stdc++.h>
using namespace std;
bool ib = ios_base::sync_with_stdio(0);
bool it = cin.tie(0);
bool check(string s)
{
bool flag = true;
for (auto &i : s)
{
if (i != 'A' and flag)
flag = false;
if (i != 'B' and !flag)
return false;
}
return true;
}
signed main()
{
int t, size, res;
cin >> t;
string s;
while (t--)
{
cin >> size >> s;
if (check(s))
res = 0;
else
{
vector<int> v;
char ch = 'A';
int cnt = 0;
for (auto &i : s)
{
if (i == ch)
++cnt;
else
--cnt;
v.push_back(cnt);
}
cnt = 0;
int br = max_element(v.begin(), v.end()) - v.begin();
for (int i = 0; i <= br; i++)
if (s[i] != 'A')
++cnt;
for (int i = br + 1; i < s.size(); i++)
if (s[i] != 'B')
++cnt;
res = cnt;
cnt = 0;
for (auto &i : s)
if (i == 'A')
++cnt;
res = min(res, cnt);
}
cout << res << '\n';
}
return 0;
}