본문 바로가기
백준/10001 - 15000

[백준] 11117번 : Letter Cookies(JAVA)

by lms0806 2021. 8. 3.
728x90
반응형

https://www.acmicpc.net/problem/11117

 

11117번: Letter Cookies

The first line of the input consists of a single number T, the number of letter cookie boxes your sister has. Each test case starts with a line describing all the letters in this box, in no particular order. Then follows a line with W, the number of words

www.acmicpc.net

풀이

처음에 테스트케이스 수를 입력받습니다. (문자 뒤에 공백 1칸 잇으니 주의!)

 

알고있는 문자열을 입력받습니다.

알고있는지 체크하기 위한 테스트케이스 수를 입력받고 그 수만큼 문자열을 입력받습니다.

알고있으면 "YES" 아니면 "NO"를 출력하면 됩니다.

 

int[] alpha = new int[26]하고, charAt()와 clone을 알고있으면 쉽게 풀 수 있는 문제였습니다.

 

소스코드

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)); 
		
		int size = Integer.parseInt(br.readLine().trim());
		
		StringBuilder sb = new StringBuilder();
		while(size --> 0) {
			String s = br.readLine();
			
			int[] alpha = new int[26];
			
			for(char ch : s.toCharArray()) {
				alpha[ch - 'A']++;
			}
			
			int size1 = Integer.parseInt(br.readLine());
			while(size1 --> 0) {
				int[] num = alpha.clone();
				sb.append(solve(br.readLine(), num)).append("\n");
			}
		}
		System.out.print(sb);
	}
	
	public static String solve(String s, int[] alpha) {
		for(char ch : s.toCharArray()) {
			if(alpha[ch - 'A']  == 0) {
				return "NO";
			}
			else {
				alpha[ch - 'A']--;
			}
		}
		return "YES";
	}
}
728x90
반응형

댓글