문자열 문제(10808, 1157, 2941)

jihyeon kim·2026년 1월 29일

코딩테스트

목록 보기
28/33

핵심 개념

개념사용법예시적용 문제
알파벳 → 인덱스c - 'a' (소문자)
c - 'A' (대문자)
'c' - 'a' → 2
'C' - 'A' → 2
10808, 1157
인덱스 → 알파벳(char)(i + 'a')
(char)(i + 'A')
(char)(2 + 'A') → 'C'1157
대소문자 변환toUpperCase()
toLowerCase()
"hello".toUpperCase() → "HELLO"1157
문자열 치환replace(old, new)"dz=ak".replace("dz=", "!") → "!ak"2941
배열 카운팅arr[index]++alphabet[c - 'a']++10808, 1157

코드

10808 코드

package A0study;

import java.io.*;

public class p10808_알파벳개수 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int[] alphabet = new int[26];
        char[] c = br.readLine().toCharArray();

        for(int i=0; i<c.length; i++) {
            alphabet[c[i] - 'a']++;
        }

        for(int i=0; i<26; i++) {
            System.out.print(alphabet[i] + " ");
        }
    }
}

1157 코드

package A0study;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class p1157_단어공부 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        char[] c = br.readLine().toUpperCase().toCharArray();
        int[] alphabet = new int[26];

        // 1. 각 알파벳 개수 세기
        for (char value : c) {
            alphabet[value - 'A']++;
        }

        // 2. 최댓값 찾기
        int max = 0;
        for(int i=0; i<26; i++) {
            if(alphabet[i] > max) {
                max = alphabet[i];
            }
        }

        // 3. 최댓값인 알파벳 찾기 & 중복 체크
        char result = '?';
        int cnt = 0;
        for(int i=0; i<26; i++) {
            if(alphabet[i] == max) {
                cnt++;
                result = (char) (i + 'A');
            }
        }

        // 4단계: 최댓값이 2개 이상이면 ?
        if(cnt > 1) {
            System.out.println('?');
        } else {
            System.out.println(result);
        }
    }
}

2941 코드

package A0study;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class p2941_크로아티아알파벳 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();

        String[] croatia = {"c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z="};

        for(String str : croatia) {
            input = input.replace(str, "!");
        }

        System.out.println(input.length());
    }
}

0개의 댓글