🕸 Algorithm/🕸 백준 BaekJoon

[백준][java][1316][그룹 단어 체커]

yeun.log 2024. 2. 13. 00:11
반응형

import java.util.Scanner;

// 1316	그룹 단어 체커
public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);

		int n = Integer.parseInt(in.nextLine());
		int res = 0;

		String[] sArr = new String[n];

		for (int i = 0; i < sArr.length; i++) {
			sArr[i] = in.nextLine();

			String word = sArr[i];
			
			boolean gw = true;
			
			StringBuffer sb = new StringBuffer();
			
			for (int j = 0; j < word.length(); j++) {
				if (word.charAt(j) != (j + 1 != word.length() ? word.charAt(j + 1) : word.length())) {
					sb.append(word.charAt(j));
				}
			}
			
			word = sb.toString();

			for (int j = 0; j < word.length(); j++) {
				String c = "" + word.charAt(j);
				String lastWord = word.substring(j + 1, word.length());

				if (lastWord.indexOf(c) > -1) {
					gw = false;
				}
			}
			
			if (gw) {
				res++;
			}
		}

		System.out.print(res);
	}
}

단어가 주어졌을 때

  • happy의 p처럼 연달아 중복이 아니라
  • happyp 처럼 뒤에 p가 따로 떨어져 붙으면 그룹단어가 아닙니다.
  • 먼저 happy -> hapy 이런식으로 연달아 중복되는 문자를 제거해 재조합한 후
  • 다시 문자가 나오지 않는 경우만 그룹단어의 갯수로 세어주었습니다.
반응형