๐
Word Count
Word Count์ฌ์ ๋ฌธ์์ด +10pts
Problem
Write a function that counts the number of words in a string.
Examples
Input:
wordCount("Hello World")Output:
2Explanation
์ด ๋ฌธ์ ๋ **split()๊ณผ ์ ๊ทํํ์**์ ์ฌ์ฉํ์ฌ ๋จ์ด ๊ฐ์๋ฅผ ์ธ๋ ๋ฐฉ๋ฒ์ ํ์ตํฉ๋๋ค. ## ํต์ฌ ๊ฐ๋ : ๋ฌธ์์ด ๋ถ๋ฆฌ์ ์นด์ดํ ๋ฌธ์์ด์ ๋จ์ด๋ก ๋ถ๋ฆฌํ ํ ๋ฐฐ์ด์ ๊ธธ์ด๋ฅผ ๊ตฌํฉ๋๋ค. ### ์ ๊ทผ ๋ฐฉ๋ฒ ```javascript str.trim().split(/\s+/).length ``` 1. trim(): ์ ๋ ๊ณต๋ฐฑ ์ ๊ฑฐ 2. split(/\s+/): ํ๋ ์ด์์ ๊ณต๋ฐฑ์ผ๋ก ๋ถ๋ฆฌ 3. length: ๋ฐฐ์ด ๊ธธ์ด = ๋จ์ด ์ ### ์ ๊ทํํ์ \s+ - \s: ๊ณต๋ฐฑ ๋ฌธ์ (์คํ์ด์ค, ํญ, ์ค๋ฐ๊ฟ ๋ฑ) - +: ํ๋ ์ด์ ``...
View detailed explanation โKey Concepts
split ๋ฉ์๋ ์ ๊ทํํ์ ๋จ์ด ์ธ๊ธฐ ๋น ๋ฌธ์์ด ์ฒ๋ฆฌ
Time: O(n) Space: O(n)
solution.js
Ctrl + Enter
Run tests to see results here.