aboutsummaryrefslogtreecommitdiff
path: root/2017/day3/balancing.cpp
blob: c5fa7c642822e4af9594bab029ed0205bca73a1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <bits/stdc++.h>
using namespace std;

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	string S; cin >> S;
	int N = S.size(), cnt = 0;
	if (count(begin(S), end(S), 'G') << 1 != N) {
		cout << "NO\n";
		return 0;
	}
	unordered_set<int> M[2];
	for (int i = 0; i < N; ++i) {
		if (S[i] != S[(i-1+N)%N]) M[S[i] == 'G'].insert(cnt);
		cnt += (S[i] == 'G' ? 1 : -1);
		if (S[i] != S[(i+1)%N] && M[S[i] == 'G'].find(cnt - (S[i] == 'G' ? 1 : -1)) != end(M[S[i] == 'G'])) {
			cout << "YES\n";
			return 0;
		}
	}
	cout << "NO\n";
	return 0;
}