💨
CODE FESTIVAL 2017 Fianl | B - Palindrome-phobia
問題
考えたこと
Sは3種類の文字(
文字列の長さを
この時、
コード
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
const int MOD = 1e9 + 7;
bool solve(unordered_map<ll, ll> &cnt, ll n) {
if (cnt.size() == 1) {
return n == 1;
}
if (cnt.size() == 2) {
return n == 2;
}
int a = cnt[0];
int b = cnt[1];
int c = cnt[2];
return abs(a - b) <= 1 && abs(b - c) <= 1 && abs(a - c) <= 1;
}
int main() {
string s;
cin >> s;
unordered_map<ll, ll> cnt;
for (int i = 0; i < s.size(); i++) {
cnt[s[i] - 'a']++;
}
cout << (solve(cnt, s.size()) ? "YES" : "NO") << endl;
}
Discussion