백준 1543번 문서 검색 JAVA

YB·2025년 2월 17일

링크텍스트

설명

indexOf 메서드를 사용하여 word가 document 내에서 발견될 때마다 반복적으로 찾고 substring을 사용하여 발견된 단어 이후의 문자열로 document를 재설정하였다.
시간복잡도: O(N²), 공간복잡도: O(N)

코드

import java.util.*;
import java.io.*;

class Main {
	public static void main (String[] args) throws IOException {
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    
	    String document = br.readLine();
	    String word = br.readLine();

		int count = 0;
		int index = 0;

		while(document.indexOf(word,index) != -1){
			count++;
			document = document.substring(document.indexOf(word) + word.length(), document.length());
		}

	    System.out.println(count);
	}
}

profile
안녕하세요

0개의 댓글