aboutsummaryrefslogtreecommitdiff
path: root/17/day3/balancing.cpp
diff options
context:
space:
mode:
Diffstat (limited to '17/day3/balancing.cpp')
-rw-r--r--17/day3/balancing.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/17/day3/balancing.cpp b/17/day3/balancing.cpp
new file mode 100644
index 0000000..c5fa7c6
--- /dev/null
+++ b/17/day3/balancing.cpp
@@ -0,0 +1,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;
+} \ No newline at end of file