๐Ÿ“œ

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.