곽로그

[백준 1326, 자바] 그룹단어 체커 -- 다시 본문

알고리즘/백준

[백준 1326, 자바] 그룹단어 체커 -- 다시

일도이동 2020. 3. 12. 01:25
반응형

문제

 

접근

 

코드

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

public class Main {
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		int N = Integer.parseInt(br.readLine());
		int count = 0; 
		for(int i = 0; i< N ;i++) {
			String word = br.readLine();
			if(isGroupWord(word)) {
				count ++;
			}
		}
		bw.write(Integer.toString(count));
		bw.close();
	}
	public static boolean[] alreadyExist;
	public static boolean isGroupWord(String word) {
		alreadyExist = new boolean[26];
		
		int length = word.length();
		int standIndex = 0;	
		int nextIndex = 0;  
		char standAlpha ; 
		char nextAlpha ;
		
		
		while(standIndex<length) {
			standAlpha = word.charAt(standIndex);
			if(alreadyExist[standAlpha-97]) {
				return false;
			}
			else {
				alreadyExist[standAlpha-97]=true;
			}
			if(standIndex == length-1) {
				return true;
			}
			
			nextIndex = standIndex+1;
			//그 다음 기준 알파벳을 찾기
			while(nextIndex<length) {
				nextAlpha = word.charAt(nextIndex);
				if(standAlpha == nextAlpha) {
					nextIndex++;
				}
				else {
					standIndex = nextIndex;
					break;
				}
			}
			//nextIndex가 word의 길이를 넘어간 경우
			if(nextIndex>=length) {
				return true;
			}
		}
		//standIndex가 word의 길이를 넘어간 경우
		return true;
	}
}

 

개선

참고블로그

 

https://hongku.tistory.com/244

 

백준/1316번 :: 그룹 단어 체커(Java 자바 구현) 알고리즘 풀이

※ 문제 풀이 코드는 맨 아래에 있습니다. 문제 그룹 단어란 단어에 존재하는 모든 문자에 대해서, 각 문자가 연속해서 나타나는 경우만을 말한다. 예를 들면, ccazzzzbb는 c, a, z, b가 모두 연속해서 나타나고,..

hongku.tistory.com

https://zorba91.tistory.com/108

 

[java] 백준 알고리즘 1316번 그룹 단어 체크 풀이

* 풀이 소스 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 27 28 29 30 31 32 33 34 35 36 public class Baekjoon1316 { static StringTokenizer st; public static void main(String..

zorba91.tistory.com

 

반응형
Comments