백준 11478번 서로 다른 부분 문자열의 개수 JAVA

YB·2025년 2월 2일

링크텍스트

설명

Set과 substring을 사용해 풀 수 있는 문제이다.
시간복잡도: 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));
	    HashSet<String> hs = new HashSet<>();
	    
	    String s = br.readLine();
	    
	    for(int i=0;i<s.length();i++){
	        for(int j=i+1;j<=s.length();j++){
                hs.add(s.substring(i, j));
            }
	    }

        System.out.println(hs.size());
	}
}

profile
안녕하세요

0개의 댓글