๐
Truncate String
Truncate String์ฌ์ ๋ฌธ์์ด +10pts
Problem
Write a function that truncates a string to a given length and adds "..." if truncated.
Examples
Input:
truncate("Hello World", 5)Output:
"Hello..."Explanation
์ด ๋ฌธ์ ๋ **์กฐ๊ฑด๋ถ ๋ฌธ์์ด ์ฒ๋ฆฌ**์ **ํ ์คํธ ๋ง์ค์** ํจํด์ ํ์ตํฉ๋๋ค. UI์์ ์์ฃผ ์ฌ์ฉ๋๋ ๊ธฐ๋ฅ์ ๋๋ค. **์๊ณ ๋ฆฌ์ฆ ๋์** 1. ๋ฌธ์์ด ๊ธธ์ด๊ฐ maxLength ์ดํ๋ฉด ๊ทธ๋๋ก ๋ฐํ 2. ๊ทธ๋ ์ง ์์ผ๋ฉด maxLength๊น์ง ์๋ฅด๊ณ "..." ์ถ๊ฐ **"Hello World", maxLength=5 ์ฒ๋ฆฌ** - "Hello World".length (11) > 5 โ ์๋ฅด๊ธฐ ํ์ - slice(0, 5) โ "Hello" - "Hello" + "..." โ "Hello..." **slice(0, maxLength)์...
View detailed explanation โKey Concepts
slice ๋ฉ์๋ ์กฐ๊ฑด๋ถ ์ฒ๋ฆฌ ๋ฌธ์์ด ์ฐ๊ฒฐ ๋ง์ค์ ํจํด
Time: O(n) Space: O(n)
solution.js
Ctrl + Enter
Run tests to see results here.