[Java] SWEA 1213 String

Lee GaEun·2024년 11월 13일

[Java] 알고리즘

목록 보기
15/93

1213 String 문제 링크

문제분석

  • 주어지는 영어 문장에서 특정한 문자열의 개수를 반환
  • 예시
    • Starteatingwellwiththeseeighttipsforhealthyeating,whichcoverthebasicsofahealthydietandgoodnutrition.
    • 위 문장에서 ti 를 검색하면, 답은 4

제약 사항

  • 테스트 케이스 수는 10
  • 문장의 길이는 1000자 이하
  • 검색하는 문자열의 길이는 10자 이하
  • 한 문장에서 하나의 문자열만 검색

입력 조건

  • 첫째 줄 : 테스트 케이스 번호
  • 둘째 줄 : 검색하는 문자열
  • 셋째 줄 : 문장

출력 조건

#부호 + 테스트 케이스 번호 + " " + 검색된 문자열 개수


#1


import java.util.Scanner;
import java.io.FileInputStream;

class Solution
{
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        /*int T;
        T=sc.nextInt();*/

        for(int test_case = 1; test_case <= 10; test_case++)
        {
            int N = sc.nextInt();
            String remind = sc.next();
            String total = sc.next();

            String newTotal = total.replaceAll(remind, "");
            int answer = (total.length()- newTotal.length())/remind.length();

            System.out.println("#" + N + " " + answer);
        }
    }
}

  • 성공(6M)
  • 싸피에 이런 게 나올리 없음..

5/2 다시 풀어봄

#2

5분


import java.util.HashMap;
import java.util.Scanner;
import java.io.FileInputStream;

class Solution
{
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        int T;
        T=10;

        for(int test_case = 1; test_case <= T; test_case++)
        {
            int N = sc.nextInt();
            String F = sc.next();
            String Hole = sc.next();

            int original = Hole.length();
            Hole = Hole.replaceAll(F, "");
            int answer = (original - Hole.length()) / F.length();
            System.out.println("#" + test_case + " " + answer);
        }
    }
}
  • 이전 풀이랑 진짜 똑같이 풀었네..
  • 걸린 시간도 똑같음
profile
I will give it my all (๑•̀o•́๑)ง

0개의 댓글