๐Ÿ“œ

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.