그룹 단어 체커 (1316)

Created:

Baekjoon No.1316 문제
Baekjoon No.1316 예제
그룹 단어의 개수를 구하라.

Bash

Bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
read n
cnt=0
for ((i=0; i<n; i++)); do
	read str
	bak=""
	group=1
	while [ "$str" != "" ]; do
		x=${str::1}
		if [[ "$bak" != *"$x"* ]]; then
			bak=$x$bak
		elif [ "$x" != "${bak::1}" ]; then
			group=0
			break
		fi
		str="${str:1}"
	done
	((cnt += group ? 1 : 0))
done
echo $cnt

Node.js

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const [n, ...arr] = require("fs").readFileSync(0).toString().trim().split("\n");
cnt = 0;
for (let str of arr) {
	let arr = [];
	group = true;
	for (let x of str) {
		if (arr.indexOf(x) == -1) {
			arr.push(x);
		} else if (arr[arr.length-1] != x) {
			group = false;
			break
		}
	}
	if (group) cnt++;
}
console.log(cnt);

Python3

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
n = int(input())
cnt = 0
for i in range(n):
    a = input()
    arr = []
    group = True
    for x in a:
        if x not in arr:
            arr.append(x)
        elif and arr[-1] != x:
            group = False
            break
    if group: cnt += 1
print(cnt)

Ruby

Ruby
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
n = gets.chomp.to_i
cnt = 0
for i in 0...n
  str = gets.chomp.split("")
  arr = []
  group = true
  for x in str
    if !arr.index(x)
      arr.append(x)
    elsif arr[-1] != x
      group = false
      break
    end
  end
  cnt += 1 if group
end
puts cnt