class Solution {
fun solution(my_string: String): IntArray {
val alphabetList = ('A'..'Z').toList() + ('a'..'z').toList()
val charFrequency = my_string.groupingBy { it }.eachCount()
return alphabetList.map { charFrequency[it] ?: 0 }.toIntArray()
}
}
class Solution {
fun solution(my_string: String): IntArray {
return (('A'..'Z').map { alpha -> my_string.count { it == alpha } } +
('a'..'z').map { alpha -> my_string.count { it == alpha } }).toIntArray()
}
}
주어진 코드에서 count의 역할은 문자열(my_string)에서 특정 문자의 개수를 세는 것입니다.
역할 설명
my_string.count { it == alpha }
my_string의 각 문자(it)가 alpha와 같은지 확인합니다.
조건(it == alpha)을 만족하는 문자의 개수를 세어 반환합니다.
동작 방식
('A'..'Z')와 ('a'..'z') 범위의 각각의 문자(alpha)에 대해 count를 호출합니다.
예를 들어, alpha가 'A'라면 my_string에서 'A'가 몇 번 등장하는지 세어 반환합니다.
간략한 요약
count는 문자열에서 특정 조건(예: 특정 문자와 일치)을 만족하는 요소의 개수를 세는 데 사용됩니다.
이 코드에서는 대문자와 소문자의 각각의 등장 횟수를 계산하는 역할을 합니다.
groupingBy
val words = listOf("apple", "banana", "cherry", "apple")
val grouped = words.groupingBy { it.first() } // 첫 글자로 그룹화
eachCount
val words = listOf("apple", "banana", "cherry", "apple")
val counts = words.groupingBy { it.first() }.eachCount()
println(counts) // {a=2, b=1, c=1}
count는
val text = "hello"
val totalCount = text.count() // 전체 문자 개수: 5
val lCount = text.count { it == 'l' } // 'l'의 개수: 2