BOJ[자바] 9935

문딤·2022년 9월 19일
0

문자열 폭발

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

소스코드


public class BOJ9935 {

    public static void main(String[] args) throws IOException {


        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String word= br.readLine();
        String bomb = br.readLine();

        int t = bomb.length();

        Stack<Character> st = new Stack<>();

        for (int i = 0; i <word.length() ; i++) {
            st.push(word.charAt(i));

            //스택에 값을 넣고 BOMB이랑 사이즈가 같아지면 비교
            if(st.size() >= t){
                boolean check =true;

                for (int j = 0; j < t; j++) {

                    int index =st.size()-t;
                    
                    if(st.get(index + j) != bomb.charAt(j)){
                        check = false;
                        break;
                    }
                }
                // check 후 true 면 pop();
                if(check){
                    for (int j = 0; j < t; j++) {
                        st.pop();
                    }
                }
            }

        }
		//스택에 남아있는 애들 출력 
        StringBuilder sb = new StringBuilder();
        for (Character c: st){
            sb.append(c);
        }
        System.out.println(sb.length() > 0 ? sb.toString() : "FRULA" );

    }
}

생각할 것

  1. 해당 stack에 있는 value와 bomb 문자열의 문자랑 어떻게 비교할 건지
  2. pop 시킨 stack의 글자를 어떻게 String으로 붙여서 보여줄것인지.

참고

profile
풀스택개발자가 될래요

0개의 댓글