๐Ÿค ์€์ง€log ๐Ÿค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด ๋ณธ๋ฌธ

๐ŸŒˆ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค:Javascript] ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด

Eun_zii 2024. 3. 19. 16:02

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.0

- ์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด

 

๐Ÿ’š ๋ฌธ์ œ์„ค๋ช…

  • ์–ด๋–ค ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด์„œ ์ ‘๋ฏธ์‚ฌ๋Š” ํŠน์ • ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, "banana"์˜ ๋ชจ๋“  ์ ‘๋ฏธ์‚ฌ๋Š” "banana", "anana", "nana", "ana", "na", "a"์ž…๋‹ˆ๋‹ค.
    ๋ฌธ์ž์—ด my_string์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, my_string์˜ ๋ชจ๋“  ์ ‘๋ฏธ์‚ฌ๋ฅผ ์‚ฌ์ „์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ๋ฌธ์ž์—ด ๋ฐฐ์—ด์„ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”.

 

๐Ÿ’š ์ œํ•œ์กฐ๊ฑด

  • my_string์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • 1 ≤ my_string์˜ ๊ธธ์ด ≤ 100

 

๐Ÿ’š ํ’€์ด

function solution(my_string) {
    const answer = []
    for ( let i = 0; i < my_string.length; i++ ) {
        answer.push(my_string.substring(i, my_string.length))
    }
    return answer.sort()
}

 

โœ… 

for๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์˜ ๊ฐ ์œ„์น˜์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๋ชจ๋“  ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•ด์ค€ํ›„,

substring( ) ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ my_string์˜ ์ธ๋ฑ์Šค i๋ถ€ํ„ฐ ๋๊นŒ์ง€์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์ถ”์ถœํ•˜์—ฌ answer ๋ฐฐ์—ด์— push( )๋กœ ๋”ํ•ด์ค€๋‹ค.

๋ชจ๋“  ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋‹ด์€ ๋ฐฐ์—ด answer๋ฅผ ์ •๋ ฌํ•ด์„œ return ํ•ด์ค€๋‹ค.

728x90