[Unrated] Dedupe - 5357
5357번: Dedupe
Redundancy in this world is pointless. Let’s get rid of all redundancy. For example AAABB is redundant. Why not just use AB? Given a string, remove all consecutive letters that are the same.
www.acmicpc.net
성능 요약
메모리: 17728 KB, 시간: 196 ms
분류
구현, 문자열
문제 설명
Redundancy in this world is pointless. Let’s get rid of all redundancy. For example AAABB is redundant. Why not just use AB? Given a string, remove all consecutive letters that are the same.
입력
The first line in the data file is an integer that represents the number of data sets to follow. Each data set is a single string. The length of the string is less than 100. Each string only contains uppercase alphabetical letters.
출력
Print the deduped string.
답 : 이전 문자와 비교하여 중복 문자를 제거하고, 저장한 뒤 출력한다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // 데이터 셋의 수 입력 받기
sc.nextLine(); // 개행 문자 처리
for (int i = 0; i < n; i++) {
String string = sc.nextLine(); // 문자열 입력 받기
// 중복 제거
StringBuilder sb = new StringBuilder();
sb.append(string.charAt(0)); // 첫 번째 문자는 그대로 저장
for (int j = 1; j < string.length(); j++) {
if (string.charAt(j) != string.charAt(j-1)) {
sb.append(string.charAt(j));
}
}
System.out.println(sb.toString());
}
sc.close();
}
}
'algorithm > 백준' 카테고리의 다른 글
[백준] Hurra! - 26767 Java[자바] (0) | 2023.03.28 |
---|---|
[백준] SASA 모형을 만들어보자 -23825 Java[자바] (0) | 2023.03.27 |
[백준] CAPS - 15000 Java[자바] (0) | 2023.03.25 |
[백준] 사장님 도박은 재미로 하셔야 합니다 - 23795 Java[자바] (0) | 2023.03.24 |
[백준] 공백 없는 A+B - 15873 Java[자바] (0) | 2023.03.23 |