제한 사항 및 입출력을 보고 생각할 수 있는 점은 아래와 같다.
1. result에 유저 아이디에 대한 최신 닉네임을 보여줘야 한다.
2. Enter, Leave는 result가 있지만 Change는 없다.
1. result에 유저 아이디에 대한 최신 닉네임을 보여줘야 한다.
2. Enter, Leave는 result가 있지만 Change는 없다.
fun solution(record: Array<String>): Array<String> {
val id = HashMap<String, String>()
return record.map {
val split = it.split(" ")
val op = split[0]
val userId = split[1]
when(op){
"Enter", "Change" -> id[userId] = split[2]
else ->""
}
split
}.asSequence()
.filter { it[0] != "Change" }
.map {
val userId = it[1]
when (it[0]) {
"Enter" -> "${id[userId]}님이 들어왔습니다."
"Leave" -> "${id[userId]}님이 나갔습니다."
else -> throw IllegalArgumentException()
}
}.toList().toTypedArray()
}
체이닝을 이용한다는 점은 비슷했으나 가장 큰 차이점으로 중간에 있는 asSequence를 이용한다는 점이다.
class Solution {
fun solution(record: Array<String>): Array<String> {
val id = HashMap<String, String>()
return record.map {
it.split(" ")
}.map {
val op = it[0]
val userId = it[1]
if (op != "Leave")
id[userId] = it[2]
when (op) {
"Enter" -> "$op $userId"
"Leave" -> "$op $userId"
else -> null
}
}.mapNotNull {
it?.split(" ")
}.map {
val op = it[0]
val userId = it[1]
when (op) {
"Enter" -> "${id[userId]}님이 들어왔습니다."
"Leave" -> "${id[userId]}님이 나갔습니다."
else -> ""
}
}.toTypedArray()
}
}