우당탕탕 개발일지

[SWEA] 1213. [S/W 문제해결 기본] 3일차 - String(Java, D3) 본문

코테/SW Expert Academy

[SWEA] 1213. [S/W 문제해결 기본] 3일차 - String(Java, D3)

ujin302 2024. 5. 13. 18:15
반응형

문제

Starteatingwellwiththeseeighttipsforhealthyeating,whichcoverthebasicsofahealthydietandgoodnutrition.
위 문장에서 ti 를 검색하면, 답은 4이다.

 

찾아야할 문자열과 문장이 주어진다. 

찾아야할 문자열이 문장에서 몇번 등장하는지 구하여라 

 

입출력 예

 

풀이

replace 를 통해서 아주 쉽게 구할 수 있다. 

주어진 문자열을 특수문자로 변경 후, 특수문자의 개수를 구하면 된다. 

 

코드

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 tc =1; tc<1+t; tc++) {
			int result=0;
			int i= sc.nextInt();
			String findStr ="", str ="";
			
			findStr = sc.next();
			str = sc.next();
			
			
			char[] charStr = str.replace(findStr, "!").toCharArray();
			for(char c : charStr) {
				if(c == '!') {
					result++;
				}
			}
			
			System.out.println("#"+i+ " " + result);
		}
	}
}

 

 

SWEA 문제

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14P0c6AAUCFAYi

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

 

반응형