-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrute.cpp
53 lines (49 loc) · 890 Bytes
/
brute.cpp
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
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define vi vector<int>
bool isbalanced(string s)
{
int cntf = 0, cntw = 0;
for (char c : s)
{
if (c == 'w')
cntw++;
else
cntf++;
}
return cntf == cntw;
}
void solve()
{
int n;
cin >> n;
string s;
cin >> s;
int sum = 0;
int maxi = 0;
// O(n^3) solution
for (int len = n; len > 0; len--)
{
for (int start = 0; start + len - 1 < n; start++)
{
string sub = s.substr(start, len);
if (isbalanced(sub))
{
cout << len << endl;
return;
}
}
}
cout << 0 << endl;
}
int main()
{
int t;
cin >> t;
while (t--)
{
solve();
}
}