[Kotlin] Korean Coding Test - Programmers - babbling

Kotlin

Language :

Question

Link

She is taking care of her six-month-old nephew.

My nephew can only pronounce the four pronunciations "aya", "ye", "woo", and "ma" together at most once.

When string array babbling is given as a parameter, complete the solution function to return the number of words that awkward nephews can pronounce.

Restrictions

In each string of babbling, "aya", "ye", "woo", and "ma" appear only once each.

In other words, "aya", "ye", "woo", and "ma" appear only once out of all possible substring in each string.

The string consists of only lower case letters.

Input/Output Example

Input/Output Example #1

  • Input: ["aya", "yee", "u", "maa", "wyeoo"]
  • Output: 1

Input/Output Example #2

  • Input: ["ayaye", "uuuma", "ye", "yemawoo", "ayaa"]
  • Output: 3

Code 1. Regular expression not used

kotlin

class Solution {
    fun solution(babbling: Array<String>): Int {
        var answer = 0
        val keyword = listOf("aya", "ye", "woo", "ma")
        babbling.forEach { str ->
            var tmp = str
            keyword.forEach {
                tmp = tmp.replace(it, " ")
            }
            if (tmp.trim().isEmpty()) answer++
        }
        return answer
    }
    
}

text

TEST 1 〉	Pass (40.90ms, 64.6MB)
TEST 2 〉	Pass (22.53ms, 65MB)
TEST 3 〉	Pass (27.24ms, 64.7MB)
TEST 4 〉	Pass (24.95ms, 65.4MB)
TEST 5 〉	Pass (26.33ms, 65.2MB)
TEST 6 〉	Pass (32.73ms, 64MB)
TEST 7 〉	Pass (34.32ms, 64.5MB)
TEST 8 〉	Pass (33.55ms, 64.1MB)
TEST 9 〉	Pass (33.85ms, 64.7MB)
TEST 10 〉	Pass (25.76ms, 65.2MB)
TEST 11 〉	Pass (31.77ms, 64.7MB)
TEST 12 〉	Pass (24.11ms, 64.5MB)
TEST 13 〉	Pass (32.54ms, 65.1MB)
TEST 14 〉	Pass (34.30ms, 63.8MB)
TEST 15 〉	Pass (25.05ms, 64.7MB)
TEST 16 〉	Pass (25.60ms, 65.2MB)
TEST 17 〉	Pass (26.95ms, 64.6MB)

Code 2. Use regular expressions

kotlin

class Solution {
    fun solution(babbling: Array<String>): Int {
        var answer = 0
        val regex = "aya|ye|woo|ma".toRegex()
        babbling.forEach {
            val tmp = it.replace(regex, "")
            if (tmp.isEmpty()) answer++
        }
        return answer
    }
}

text

TEST 1 〉	Pass (2.32ms, 59.6MB)
TEST 2 〉	Pass (3.49ms, 61.5MB)
TEST 3 〉	Pass (1.91ms, 61.8MB)
TEST 4 〉	Pass (3.20ms, 59.4MB)
TEST 5 〉	Pass (2.83ms, 59.9MB)
TEST 6 〉	Pass (1.84ms, 59.4MB)
TEST 7 〉	Pass (2.69ms, 58.9MB)
TEST 8 〉	Pass (2.20ms, 61MB)
TEST 9 〉	Pass (2.41ms, 59.9MB)
TEST 10 〉	Pass (1.48ms, 61MB)
TEST 11 〉	Pass (1.29ms, 61.5MB)
TEST 12 〉	Pass (1.25ms, 60.8MB)
TEST 13 〉	Pass (1.86ms, 58.9MB)
TEST 14 〉	Pass (1.31ms, 62.1MB)
TEST 15 〉	Pass (1.30ms, 62.2MB)
TEST 16 〉	Pass (1.37ms, 60.4MB)
TEST 17 〉	Pass (1.36ms, 61.2MB)

민갤

Back-End Developer

백엔드 개발자입니다.