hiro1729 競プロ

競プロの解説などを書きます。

ABC327-A 解説

A - ab

abが隣接する」は「abまたはbaが含まれる」と言い換えられるので、そのまま書きます。

Python

N = int(input())
S = input()
print("Yes" if "ab" in S or "ba" in S else "No")

C++

#include <iostream>
using namespace std;

int main() {
  int N;
  string S;
  cin >> N >> S;
  bool ok = false;
  for (int i = 0; i < N - 1; i++) {
    if (S[i] == 'a' && S[i + 1] == 'b') {
      ok = true;
    }
    if (S[i] == 'b' && S[i + 1] == 'a') {
      ok = true;
    }
  }
  if (ok) cout << "Yes";
  else cout << "No";
}