본문 바로가기

Problem Solving/백준

백준 2941번

반응형

1. 주어진 단어안에 크로아티아 문자들이 있다면 *로 바꿔주고 몇개 들어있는지 체크한다.

2. 나머지 다른 문자들의 갯수와 합쳐 출력한다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.Scanner;
public class Main {
    public static void main(String[] args)
    {
        String words = new String();
        
        Scanner sc= new Scanner(System.in);
 
        String croWords[] = new String[]{"c=","c-","dz=","d-","lj","nj","s=","z="};
                                    //크로아티아 문자들
        
        words = sc.nextLine().trim();
            
        int k =0;
        for(int i=0; i<croWords.length; i++){
            while(words.contains(croWords[i])) {//크로아티아 문자가 포함되어있다면
                words = words.replaceFirst(croWords[i],"*");//전부 *문자로 바꾼다
                k=k+1;//바꾼 횟수를 센다
            }
        }
        while(words.contains("*")) {
            words = words.replace("*","");//크로아티아 문자들을 전부 공백으로 바꿔준다
        }
        System.out.println(words.length()+k);//남아있는 문자의 갯수와 + 크로아티아 횟수를 더해서 출력
    }
}
cs


반응형

'Problem Solving > 백준' 카테고리의 다른 글

백준 2309번  (0) 2019.01.27
백준 1152번  (0) 2019.01.26
백준 1316번  (0) 2019.01.24
백준 10809번  (0) 2019.01.24
백준 2908 상수  (0) 2019.01.20