내 풀이
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str1 = sc.nextLine();
String str2 = sc.nextLine();
int maxCount = 0;
while(str1.contains(str2)){
maxCount++;
int restartIndex = str1.indexOf(str2) + str2.length();
str1 = str1.substring(restartIndex);
}
System.out.println(maxCount);
}
}
답안1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String doc = sc.nextLine();
String word = sc.nextLine();
int count = 0;
int startIndex = 0;
while(true) {
int findIndex = doc.indexOf(word, startIndex);
if (findIndex < 0)
break;
count++;
startIndex = findIndex + word.length();
}
System.out.println(count);
}
}
indexOf 이용
답안2
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String doc = sc.nextLine();
String word = sc.nextLine();
String replaced = doc.replace(word, "");
int length = doc.length() - replaced.length();
int max = length / word.length();
System.out.println(max);
}
}
replace이용
'백준 문제풀이 > 푼 문제' 카테고리의 다른 글
[백준 2744] 대소문자 바꾸기 (1) | 2024.02.15 |
---|