작성일 :

문제 링크

31616번 - 揃った文字 (Matched Letters)

설명

길이 N의 문자열이 모두 같은 문자로 이루어졌는지 판별하는 문제입니다.


접근법

첫 글자를 기준으로 나머지 문자들을 비교합니다. 하나라도 다르면 No, 끝까지 같으면 Yes를 출력합니다.


Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;

class Program {
  static void Main() {
    var n = int.Parse(Console.ReadLine()!);
    var s = Console.ReadLine()!;

    var ok = true;
    for (var i = 1; i < n; i++) {
      if (s[i] != s[0]) {
        ok = false;
        break;
      }
    }

    Console.WriteLine(ok ? "Yes" : "No");
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n; cin >> n;
  string s; cin >> s;

  bool ok = true;
  for (int i = 1; i < n; i++) {
    if (s[i] != s[0]) {
      ok = false;
      break;
    }
  }

  cout << (ok ? "Yes" : "No") << "\n";

  return 0;
}