[Java] 백준 1283번: 단축키 지정

U·2024년 10월 18일

백준

목록 보기
69/116

[문제 바로 가기] - 단축키 지정

💡 접근 방식

단순히 문자열에 대한 이해가 있으면 구현만 하면 되는 문제!
다만 이클립스 IDE를 사용하면서 toUpperCase()toLowerCase()를 사용해서 다행이지 프로그래머스 툴이었다면 사용하지 못했을 것이다..!!


풀이

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		
		int N = Integer.parseInt(br.readLine());
		Set<String> set = new HashSet<>();

		for (int i = 0; i < N; i++) {
			boolean isShortcutKey = false;
			String str = br.readLine();
			String words[] = str.split(" ");

			// 1. 단어의 첫 글자가 단축키로 지정되었는지
			for (int j = 0; j < words.length; j++) {
				String s = words[j].charAt(0) + "";
				int index = 0;
				
				if (!set.contains(s.toUpperCase()) && !set.contains(s.toLowerCase())) {
					set.add(s.toUpperCase());
					set.add(s.toLowerCase());
					isShortcutKey = true;
					
					for (int k = 0; k < j; k++) {
						index += words[k].length();
						if (index != 0) index += 1;
					}
					
					sb.append(str.substring(0, index) + "[" + str.charAt(index) + "]" + str.substring(index + 1) + "\n");
					break;
				}
			}
			
			if (isShortcutKey) continue;
			
			for (int j = 0; j < str.length(); j++) {
				String s = str.charAt(j) + "";
				
				if (!s.equals(" ") && !set.contains(s.toUpperCase()) && !set.contains(s.toLowerCase())) {
					set.add(s.toUpperCase());
					set.add(s.toLowerCase());
					isShortcutKey = true;
					
					sb.append(str.substring(0, j) + "[" + str.charAt(j) + "]" + str.substring(j + 1) + "\n");
					break;
				}
			}
			
			
			if (isShortcutKey) continue;
			
			sb.append(str + "\n");
		}
		
		System.out.println(sb);
	}
}
profile
백엔드 개발자 연습생

0개의 댓글