Baekjoon Algorithm | Problem 1316: Group Word Checker

Source

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

Problem

Solve Baekjoon Online Judge problem 1316, Group Word Checker.

Input

Follow the input format and constraints given in the original problem statement.

Output

Print the answer required by the problem.

Sample Input 1

3 happy new year

Sample Output 1

3

Sample Input 2

4 aba abab abcabc a

Sample Output 2

1

Sample Input 3

5 ab aa aca ba bb

Sample Output 3

4

Sample Input 4

2 yzyzy zyzyz

Sample Output 4

0

Sample Input 5

1 z

Sample Output 5

1

Algorithm Classification

  • Implementation
  • String

Solution

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

public class Main {

    public static void main(String[] args) throws IOException {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {

            final int n = Integer.parseInt(br.readLine());

            int count = n;
            for (int i = 0; i < n; i++) {
                String line = br.readLine();

                Set set = new HashSet<Character>();
                for (int j = 0; j < line.length(); j++) {

                    if (j != 0 && line.charAt(j) == line.charAt(j - 1)) {
                        continue;
                    }

                    char b = line.charAt(j);
                    if (set.contains(b)) {
                        count--;
                        break;
                    }
                    set.add(b);
                }
            }

            System.out.println(count);
        }
    }
}