Post

[BaekJoon] #1264 - 모음의 개수 [Java][C++]

[BaekJoon] #1264 - 모음의 개수 [Java][C++]

문제 링크


1. 아이디어

주어진 영문 문장에서 알파벳 모음의 개수를 세는 간단한 문제다. 각 문자에 대해 a, e, i, o, u와 대문자 총 10가지를 매번 비교해서 카운팅하는 방식으로 해결했다.


2. 복잡도

접근시간공간
풀이$O(T \times L)$$O(L)$

($T$ = 종료 줄을 제외한 테스트 케이스 수, $L$ = 케이스당 line의 길이의 최댓값)


3. 코드

풀이 [Java][C++]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        while (true) {
            String line = br.readLine();
            if (line.equals("#")) break;

            int cnt = 0;
            for (char c : line.toCharArray()) {
                if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') cnt++;
                if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') cnt++;
            }

            sb.append(cnt).append("\n");
        }

        System.out.println(sb);
    }
}
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(0);
    cin.tie(0);

    while (true) {
        string line;
        getline(cin, line);

        if (line == "#") break;

        int cnt = 0;
        for (char c : line) {
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') cnt++;
            if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') cnt++;
        }

        cout << cnt << '\n';
    }
}

This post is licensed under CC BY 4.0 by the author.