PS/백준
[백준] 1157번: 단어 공부_Java
codeStudy123
2022. 1. 24. 22:34
제목: 단어 공부
브론즈1
문제
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
입력
첫째 줄에 알파벳 대소문자로 이루어진 단어가 주어진다. 주어지는 단어의 길이는 1,000,000을 넘지 않는다.
출력
첫째 줄에 이 단어에서 가장 많이 사용된 알파벳을 대문자로 출력한다. 단, 가장 많이 사용된 알파벳이 여러 개 존재하는 경우에는 ?를 출력한다.
예제 입력 1 복사
Mississipi
예제 출력 1 복사
?
예제 입력 2 복사
zZa
예제 출력 2 복사
Z
예제 입력 3 복사
z
예제 출력 3 복사
Z
예제 입력 4 복사
baaa
예제 출력 4 복사
A
🔑 코드
case1:
더보기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int[] alphabet = new int[26];
for (int i = 0; i < str.length(); i++) {
if ('a' <= str.charAt(i) && str.charAt(i) <= 'z') {
alphabet[str.charAt(i) - 'a']++;
} else if ('A' <= str.charAt(i) && str.charAt(i) <= 'Z') {
alphabet[str.charAt(i) - 'A']++;
}
}
int max = 0;
char c = '?';
for (int i = 0; i < alphabet.length; i++) {
if (max < alphabet[i]) {
max = alphabet[i];
c = (char) (i + 'A');
} else if (max == alphabet[i]) {
c = '?';
}
}
System.out.println(c);
}
}
case2:
더보기
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int[] alphabet = new int[26];
method(alphabet, str);
int max = 0;
char c = '\u0000';
for (int i = 0; i < alphabet.length; i++) {
if (max < alphabet[i]) {
max = alphabet[i];
c = (char) (i + 'A');
} else if (max == alphabet[i]) {
c = '?';
}
}
System.out.println(c);
}
static int[] method(int[] a, String str) {
for (int i = 0; i < str.length(); i++) {
if ('a' <= str.charAt(i) && str.charAt(i) <= 'z') {
a[str.charAt(i) - 'a']++;
} else if ('A' <= str.charAt(i) && str.charAt(i) <= 'Z') {
a[str.charAt(i) - 'A']++;
}
}
return a;
}
}
알고리즘 분류
https://www.acmicpc.net/problem/1157
1157번: 단어 공부
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
www.acmicpc.net
대표사진
<a href="https://www.flaticon.com/kr/free-icons/" title="연산 아이콘">연산 아이콘 제작자: Flat Icons - Flaticon</a>