본문 바로가기
백준/5001 - 10000

[백준] 7120번 : String(JAVA)

by lms0806 2021. 7. 20.
728x90
반응형

https://www.acmicpc.net/problem/7120

 

7120번: String

It sometimes happens that a button on the computer keyboard sticks and then in the printed text there are more than one identical letters. For example, the word "piano" can change into "ppppppiaanooooo". Your task is to write a program that corrects these

www.acmicpc.net

풀이

체크할 문자 char를 선언합니다.

처음에 받은 문자가 체크문자랑 다를 경우 출력해주고 체크문자로 바꿔줍니다.

 

 

소스코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
		
		char c = '1';
		StringBuilder sb = new StringBuilder();
		for(char ch : br.readLine().toCharArray()) {
			if(c != ch) {
				sb.append(ch);
				c = ch;
			}
		}
		System.out.print(sb);
	}
}
728x90
반응형

댓글