[SWEA] #1213 String

KwonSC·2021년 11월 6일
0

SWEA - Java

목록 보기
7/26
post-thumbnail

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14P0c6AAUCFAYi&categoryId=AV14P0c6AAUCFAYi&categoryType=CODE


Code

import java.util.Scanner;

class Solution {
    public static void main(String args[]) throws Exception {
        Scanner sc = new Scanner(System.in);
        for (int testCase = 1; testCase <= 10; testCase++) {
            int tc = sc.nextInt();
            String find = sc.next();
            String check = sc.next();
            int result = 0;
            for (int i = 0; i <= check.length() - find.length(); i++) {
                if (check.substring(i, i + find.length()).compareTo(find) == 0) {
                    result++;
                }
            }
            System.out.printf("#%d %d\n", testCase, result);
        }
    }
}

Solution

find는 찾아야할 문자, check는 입력받은 문장
for문을 돌면서 check에서 find크기만큼의 substring과 find를 비교후 같으면 result를 더해주는 방식

0개의 댓글