📜

Count Characters

Count Characters
쉬움 문자열 +10pts

Problem

Write a function that counts occurrences of a specific character in a string.

Examples

Input: countChar("hello", "l")
Output: 2

Explanation

이 문제는 **split의 창의적 활용**으로 특정 문자의 출현 횟수를 세는 방법을 학습합니다. **split 기반 카운팅 원리** 문자열을 특정 문자로 분할하면, 분할된 조각 수는 항상 "해당 문자 개수 + 1"입니다. **"hello"에서 "l" 카운팅** - "hello".split("l") → ["he", "", "o"] - 조각 수: 3 - "l" 개수: 3 - 1 = 2 **"banana"에서 "a" 카운팅** - "banana".split("a") → ["b", "n", "n", ""] - 조각 수: 4 - "a"...

View detailed explanation →

Key Concepts

split으로 카운팅 구분자와 조각 수 관계 문자열 분할 배열 길이 활용
Time: O(n) Space: O(n)
solution.js
Ctrl + Enter
Run tests to see results here.