Home 백준 - 1316 그룹 단어 체커
Post
Cancel

백준 - 1316 그룹 단어 체커

문제

1316 그룹 단어 체커

screencapture

kotlin code: 첫 번째 방법

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fun main() {
    q1316()
}

fun q1316() {
    val inputList = List(readln().toInt()) { readln() }
    var groupWordCount = 0
    for(input in inputList) {
        if(input.length == input.toSet().size) {
            groupWordCount++
            continue
        }
        val wordList = input.groupingBy { it }.eachCount().filter { it.value > 1 }.map { it.key.toString().repeat(it.value) }
        var isGroupWord = true
        for(word in wordList) {
            if(!input.contains(word)) {
                isGroupWord = false
                break
            }
        }
        if(isGroupWord) groupWordCount++
    }
    println(groupWordCount)
}

kotlin code: 두 번째 방법

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
fun main() {
    q1316_2()
}

fun q1316_2() {
    val inputList = List(readln().toInt()) { readln() }
    var groupWordCount = 0
    for(input in inputList) {
        if(input.length == input.toSet().size) {
            groupWordCount++
            continue
        }

        var isGroupWord = true
        val set = HashSet<Char>()
        var preChar = ' '
        for(ch in input) {
            if(set.contains(ch) && preChar != ch) {
                isGroupWord = false
            }
            set.add(ch)
            preChar = ch
        }
        if(isGroupWord) groupWordCount++
    }
    println(groupWordCount)
}
This post is licensed under CC BY 4.0 by the author.

백준 - 2164 카드2

백준 - 2941 크로아티아 알파벳