๐Ÿ“œ

Word Count

Word Count
์‰ฌ์›€ ๋ฌธ์ž์—ด +10pts

Problem

Write a function that counts the number of words in a string.

Examples

Input: wordCount("Hello World")
Output: 2

Explanation

์ด ๋ฌธ์ œ๋Š” **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.