๐
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:
2Explanation
์ด ๋ฌธ์ ๋ **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.