perry05

[백준] 1316 - 그룹 단어 체커(JAVA) 본문

문제풀이 기록/JAVA

[백준] 1316 - 그룹 단어 체커(JAVA)

perry05 2022. 11. 28. 18:18

[백준] 단계별로 풀어보기 - 문자열

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

 

1316번: 그룹 단어 체커

그룹 단어란 단어에 존재하는 모든 문자에 대해서, 각 문자가 연속해서 나타나는 경우만을 말한다. 예를 들면, ccazzzzbb는 c, a, z, b가 모두 연속해서 나타나고, kin도 k, i, n이 연속해서 나타나기 때

www.acmicpc.net

 

> 문제

 

> 풀이

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

public class Main {
	public static boolean isGroupWord(String s) {
		int index;
		
		for(int i = 0; i < s.length(); i++) {
			index = s.indexOf(s.charAt(i));
			
			// 처음 나온 글자의 위치를 계속 탐색해가면서 연속되지 않으면 false return
			while(index != -1) {
				int tmp = s.indexOf(s.charAt(i), index+1);
				if(tmp==-1)
					break;
				if(index+1 != tmp)
					return false;
				index = tmp;
			}
			// 같은 단어로 불필요하게 반복할 필요 없기 때문에 i 변경
			i = index;
		}
		
		// 반복문이 무사히 끝나면 그 단어는 그룹 단어가 맞다
		return true;
	}
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n;
		int cnt = 0;
		
		n = Integer.parseInt(br.readLine());
		
		for(int i = 0; i < n; i++) {
			if(isGroupWord(br.readLine()))
				cnt++;
		}
		br.close();
		
		System.out.println(cnt);
	}
}

indexOf를 활용해 index값을 비교해가면서 문제를 해결했다.

Comments