728x90
문제
그룹 단어란 단어에 존재하는 모든 문자에 대해서, 각 문자가 연속해서 나타나는 경우만을 말한다. 예를 들면, ccazzzzbb는 c, a, z, b가 모두 연속해서 나타나고, kin도 k, i, n이 연속해서 나타나기 때문에 그룹 단어이지만, aabbbccb는 b가 떨어져서 나타나기 때문에 그룹 단어가 아니다.
단어 N개를 입력으로 받아 그룹 단어의 개수를 출력하는 프로그램을 작성하시오.
입력
첫째 줄에 단어의 개수 N이 들어온다. N은 100보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에 단어가 들어온다. 단어는 알파벳 소문자로만 되어있고 중복되지 않으며, 길이는 최대 100이다.
출력
첫째 줄에 그룹 단어의 개수를 출력한다.
String의 각 char들은, 중복된 글자가 없거나 있다면 자신과 붙어 있어야 그룹 단어가 될 수 있다.
그래서 boolean check는 옆에 붙어 있는 글자가 자신과 같지 않은 char 중, 이후에도 중복된 글자가 나오지 않는 것을 체크한다. check가 true라면 group 단어가 될 수 없기 때문에 isGroup에서 false를 리턴한다.
그렇게 for loop를 다 돌고도 isGroup이 true라면 그룹단어임을 의미하기 때문에 cnt에 1씩 더한다.
package BOJ0726;
import java.util.Scanner;
public class BOJ_1316_GroupWordChecker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int cnt = 0;
for (int n = 0; n < N; n++) {
boolean check = false;
boolean isGroup = true;
String input = sc.next();
for (int i = 0; i < input.length() - 1; i++) {
if (input.charAt(i) != input.charAt(i + 1)) {
for (int j = i + 1; j < input.length(); j++) {
if (input.charAt(i) == input.charAt(j)) {
check = true;
}
}
if (check) {
isGroup = false;
break;
}
}
}
if (isGroup) {
cnt++;
check = false;
}
}
System.out.println(cnt);
}
}
728x90
'문제 풀이 > BOJ' 카테고리의 다른 글
[백준] 1181 - 단어 정렬 (자바/Java) : Comparator (0) | 2022.07.26 |
---|---|
[백준] 2869 - 달팽이는 올라가고 싶다 (자바/Java) (0) | 2022.07.26 |
[백준] 10989 - 수 정렬하기 3 (자바/Java) : 계수 정렬(Counting Sort) (0) | 2022.07.26 |
[백준] 2750 - 수 정렬하기: (자바/Java) 선택 정렬(Selection Sort) (0) | 2022.07.26 |
[백준] 11651 - 좌표 정렬하기 2 (자바/Java) : Merge Sort (0) | 2022.07.26 |