
내가 생각했을때 문제에서 원하는부분
Input is a line containing words separated by single spaces, where a word consists of one or more uppercase letters.
A line contains no more than 80 characters.
The output is "yes" if no word is repeated, and "no" if one or more words repeat.
내가 이 문제를 보고 생각해본 부분
BufferedReader로 한 줄의 문장 입력을 받는다.
입력받은 문장을 공백 " " 기준으로 나누어 단어 배열 words를 만든다.
HashSet을 생성해서 단어를 기록하면서 중복을 체크한다.
반복문으로 배열을 하나씩 살펴본다.
이미 집합에 단어가 있으면 중복이니까 "no"를 출력하고 프로그램을 종료한다.
집합에 없다면 단어를 추가해서 다음 단어를 검사한다.
모든 단어를 순회했는데 중복이 없으면 "yes"를 출력한다.
br.close();를 호출하여 입출력 객체를 닫아 자원을 해제한다.
코드로 구현
package baekjoon.baekjoon_32;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
// 백준 15098번 문제
public class Main1288 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// 입력 라인 읽기
String line = br.readLine();
// 단어별로 나누기
String[] words = line.split(" ");
// 단어 중복 체크용 집합
Set<String> wordSet = new HashSet<>();
// 단어 순회하며 중복 여부 검사
for (String word : words) {
if (wordSet.contains(word)) {
System.out.println("no"); // 중복 발견 시 no 출력 후 종료
br.close();
return;
}
wordSet.add(word);
}
// 중복 없으면 yes 출력
System.out.println("yes");
br.close();
}
}
코드와 설명이 부족할수 있습니다. 코드를 보시고 문제가 있거나 코드 개선이 필요한 부분이 있다면 댓글로 말해주시면 감사한 마음으로 참고해 코드를 수정 하겠습니다.