Skip to content

Conversation

@sep037
Copy link
Collaborator

@sep037 sep037 commented Apr 28, 2025

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

์ ‘๋ฏธ์‚ฌ ๋ฐฐ์—ด
แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2025-04-28 17 18 25
๊ทธ๋ƒฅ ์ƒ๊ฐ ํ•  ์‹œ๊ฐ„๋“ค ๋œ ์“ฐ์‹œ๋ผ๊ณ  ์ •๋ ฌ .. ๊ฐ€์ ธ์™”์–ด์š”

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

5๋ถ„ ใ…‹

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์„ฑ์˜์—†์–ด๋ณด์ผ์ˆ˜์ž‡์ง€๋งŒ..

  1. ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.

  2. ๋นˆ ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค. (์Šฌ๋ผ์ด์‹ฑ ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์„ ๋ฐฐ์—ด)

  3. ๋ฌธ์ž์—ด ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ:
    a. ํ˜„์žฌ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋ฌธ์ž์—ด์„ ์Šฌ๋ผ์ด์‹ฑํ•œ๋‹ค.
    b. ์Šฌ๋ผ์ด์‹ฑํ•œ ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•œ๋‹ค.

  4. ๋ฐฐ์—ด์„ ์ •๋ ฌํ•œ๋‹ค.

  5. ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์ €๋Š” ๊ทธ๋ƒฅ sort๊ฐ€ ๋ชจ๋“  ๋ฌธ์ž์—ด ์ƒ๋Œ€๋กœ ์ •๋ ฌํ•˜๋Š”์ง€๋ฅผ ๊นŒ๋จน๊ณ 
๊ทธ๋ƒฅ ์ฒซ ๊ธ€์ž ์ •๋ ฌํ•˜๊ณ  ๊ทธ ๋‹ค์Œ ๋‘ ๋ฒˆ์งธ ์ •๋ ฌํ•˜๊ณ  ๊ทธ๋žฌ๋‹ค๊ฐ€ ๊ทธ๋ƒฅ sort ํ•จ์ˆ˜ ์‚ฌ์šฉํ–ˆ์–ด์šฉ
์–ด์ผ€ ์ด๊ฑธ ๊นŒ๋จน์ง€ ?

Comment on lines +15 to +17
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋Š” removeFirst() ์‚ฌ์šฉํ•ด์คฌ๋Š”๋ฐ, String.index์‚ฌ์šฉํ•ด์„œ sliceํ•˜์‹ ๊ฒŒ ๋” swift๋‹ค์šด๋А๋‚Œ์ด๋„ค์š”..!
์ด๊ฑฐ๋กœ ์„ฑ๋Šฅ๋น„๊ต๋ฅผ ํ•œ๋ฒˆํ•ด๋ดค๋Š”๋ฐ ์•„๋ž˜ ๋” ์ž์„ธํžˆ ๋‚จ๊ฒจ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค !

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bishoe01 ํ—ค์ดํ—ค์ด finn~ ์—ญ์‹œ ์ง์ด๋„ค์˜ˆ ๋ฐ‘์— ์ฝ”๋“œ ๋ณด๊ณ ์™”๋Š”๋ฐ์˜ˆ ์ œ๊ฐ€ ๋ฌธ์ž์—ด ํ’€๋•Œ๋Š” type(of:) ๋‹ค ์ฐ์–ด๋ณด๋Š” ํŽธ์ž…๋‹ˆ๋‹ค์š” !!

๊ทธ๋ž˜์„œ ๋ฌธ์ž์—ด ์Šฌ๋ผ์ด์‹ฑ์œผ๋กœ ์šฐ์งธ ์•ˆ๋ ๊นŒ ?? ํ•ด์„œ ๋ณด๋‹ˆ ์—ญ์‹œ๋‚˜ str[startIndex...]์œผ๋กœ ๊ฐ€๋Šฅํ•˜๋˜๋ฐ ์ด๊ฒŒ Substring์ด๋”๋ผ๊ตฌ์š”

๊ทธ๋ž˜์„œ ์ฒซ ๋ฐฐ์—ด์„ [Substring] ์œผ๋กœ ํ•ด์ฃผ๊ณ  O(N)์œผ๋กœ ๋˜๊ฒŒ ํ•œ๋ฒˆ ํ•ด๋ดค๋Š”๋ฐ ํ†ต๊ณผ ๋˜์—ˆ์œผ์˜ˆ

import Foundation

let str = readLine()!

var strArray: [Substring] = []

for i in 0..<str.count {
    let startIndex = str.index(str.startIndex, offsetBy: i)
    strArray.append(str[startIndex...])
//    print(type(of:str[startIndex...]))
}

for item in strArray.sorted(){
    print(item)
}

๋น„์Šทํ•˜๋ ค๋‚˜์š” ? ๋ฒค์น˜๋งˆํ‚น ๊ฐ€๋Šฅํ•˜์‹ ๊ฐ€์˜ˆ ?

Copy link
Member

@giljihun giljihun May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋Š” dropFirst() ๋ฅผ ์‚ฌ์šฉํ•ด๋ดค๋Š”๋ฐ์š”.

dropFirst()๋Š” ์•ž ๋ฌธ์ž๋ฅผ ๊ฑด๋„ˆ๋›ด Substring์„ ๋ฆฌํ„ดํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฆ‰์‹œ ๋ณต์‚ฌ๊ฐ€ ์ผ์–ด๋‚˜์ง„ ์•Š์ง€๋งŒ,
๊ทธ๊ฑธ ๋‹ค์‹œ String()์œผ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์ €์žฅํ•˜๊ฒŒ ๋˜๋ฉด, ๊ทธ ์‹œ์ ์—์„œ๋งŒ ๋ณต์‚ฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
๊ทธ๋ž˜์„œ ์ฒ˜์Œ์—” ์ด ๋ฐฉ์‹์ด ์ตœ์„ ์ด๊ฒ ๊ตฌ๋‚˜? ์‹ถ์—ˆ๋Š”๋ฐ.

๋ง์”€ํ•˜์‹  removeFirst() ๋ฐฉ์‹์ด COW ์กฐ๊ฑด์—์„œ ํฌ์ธํ„ฐ๋งŒ ์ด๋™ํ•˜๋Š” ์‹์œผ๋กœ ์ฒ˜๋ฆฌ๋ผ์„œ
์‹ค์ œ๋กœ๋Š” ๋” ๋น ๋ฅผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์€ ์ •๋ง ์ข‹์€ ๋‚ด์šฉ์ด๋„ค์š”.

์ด๊ฒƒ์ด deep dive!๊ตฐ์š”.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜คํ˜ธ ๋‹ค๋“ค ์ธ๋ฑ์Šค๋‚˜ dropFirst๋ฅผ ์‚ฌ์šฉํ•˜์…จ๊ตฐ์š”

์ €๋Š” suffix๋กœ ๋ฝ‘์•„๋‚ด์คฌ์Šต๋‹ˆ๋‹ค!

for i in 1...S.count {
    words.append(String(S.suffix(i)))
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alstjr7437 ์™€ suffix prefix์— ๋Œ€ํ•œ ๊ธ€์„ ์จ๋†“๊ณ ๋„ ๊ทธ๊ฑธ ํ•˜... ์ €์˜ ์ด ํ•˜์ฐฎ์€ ์ค‘์ƒ์˜ ๋จธ๋ฆฌํ†ต์„ ํ•œ ๋ฒˆ ํƒ ! ์น˜๊ณ  ์–ต! ํ•˜๋‹ˆ ์ดํ•ดํ•ด๋ฒ„๋ฆฌ๋„ค์šง

Copy link
Collaborator

@bishoe01 bishoe01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์„ค๋ช… ํŽธ์˜์ƒ ํ•จ์ˆ˜์ด๋ฆ„์„ ์ œ ์ด๋ฆ„์œผ๋กœ ํ–ˆ์–ด์š” !

func finn(_ str: String) {
    var answer: [String] = [str]
    var changableStr = str

    for _ in 0 ..< str.count - 1 {
        changableStr.removeFirst()
        answer.append(changableStr)
    }

    answer.sort()

    for item in answer {
        print(item)
    }
}

์ €๋Š” answer๋ฐฐ์—ด์—๋‹ค๊ฐ€ ๋ฌธ์ œ์—์„œ ์˜๋„ํ•œ๋Œ€๋กœ ์•ž์— ํ•˜๋‚˜์”ฉ
removeFirst()๋ฅผ ํ†ตํ•ด์„œ ์•ž์—์„œ ํ•œ๊ธ€์ž์”ฉ ๋–ผ๋จน์œผ๋ฉด์„œ ๋ฐฐ์—ด์— ๋„ฃ๊ณ  sorting์œผ๋กœ ๋Œ๋ ธ์–ด์š”!

์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ์•ž์—์„œ ๋–ผ์–ด๊ฐ€๋ฉด O(N)๋งŒํผ ์ด์ œ ๋’ค์—์• ๋“ค์ด ๋•ก๊ฒจ์ ธ์™€์•ผํ•˜๋Š”๊ฒƒ์œผ๋กœ ์•Œ์•„์„œ, ๋‹น์—ฐํžˆ ์ œ ์ฝ”๋“œ๊ฐ€ ๊ธ€๋กœ๋‹ˆ์ฝ”๋“œ๋ณด๋‹ค ๋А๋ฆฌ๋‹ค๊ณ  ์ƒ๊ฐ์„ ํ–ˆ๋Š”๋ฐ์š”..!

let alphabet = "abcdefghijklmnopqrstuvwxyz"
let testInput = String((0 ..< 4000).map { i in
    alphabet[alphabet.index(alphabet.startIndex, offsetBy: i % alphabet.count)]
})

var totalFinn = 0.0
var totalGlowny = 0.0

for _ in 0 ..< 100 {
    var start = CFAbsoluteTimeGetCurrent()
    finn(testInput)
    var end = CFAbsoluteTimeGetCurrent()
    totalFinn += (end - start)

    start = CFAbsoluteTimeGetCurrent()
    glowny(testInput)
    end = CFAbsoluteTimeGetCurrent()
    totalGlowny += (end - start)
}
print("Finn ํ‰๊ท : \(totalFinn / 100)์ดˆ")
print("Glowny ํ‰๊ท : \(totalGlowny / 100)์ดˆ")

์ด๊ฑฐ๋กœ ์ด์ œ ๋ฒค์น˜๋งˆํ‚น์„ ๋Œ๋ ค๋ดค๊ฑฐ๋“ ์š”!
์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๋Œ€๋กœ ๋ฐ˜๋ณต๋˜๋Š” 4000์ž ๊ธธ์ด์˜ ๋ฌธ์ž์—ด์„ ํ™œ์šฉํ•œ ๊ฒƒ์œผ๋กœ ๋Œ๋ ค๋ดค์Šต๋‹ˆ๋‹ค!
(1000์ž๋Š” ๋„ˆ๋ฌด ๋А๋ฆฐ๊ฒƒ๊ฐ™์•„์„œ 10000์ž๊นŒ์ง€ ๋Š˜๋ ธ๋”๋‹ˆ ์ด๊ฒŒ ๋„ˆ๋ฌด ๊ฐ’์ด ์•ˆ๋‚˜์˜ค๊ธธ๋ž˜ ๋Œ€๊ฐ• 4์ฒœ์ž ์ •๋„๋กœ ํ•ฉ์˜๋ฅผ ๋ดค์Šต๋‹ˆ๋‹ค..)

๊ทธ๋žฌ๋”๋‹ˆ ๊ฒฐ๊ณผ๊ฐ’์ด ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜์˜ค๋”๋ผ๊ตฌ์š”..!

Finn ํ‰๊ท : 0.009801552295684815์ดˆ
Glowny ํ‰๊ท : 0.023117616176605224์ดˆ

image
String.index์ž์ฒด๊ฐ€ ์•ž์—์„œ ์ฐจ๋ก€๋Œ€๋กœ ๋ณด๋‹ˆ๊นŒ O(i)๊ธฐ๋„ ํ•˜๊ณ ,
String(suffix)ํ• ๋•Œ ์ด๊ฒŒ ๋˜ ๋ณต์‚ฌํ•˜๋Š” ๊ฐ’์ด ๋งŽ์ด๋“ ๋‹ค๊ณ  ํ•˜๋”๋ผ๊ตฌ์š”..!

Substring to String ์—ฌ๊ธฐ์—์„œ Substring to String ์„น์…˜ ๋ณด์‹œ๋ฉด ์–ด๋–ป๊ฒŒ String()์ด ๋™์ž‘ํ•˜๋Š”์ง€ ์„ค๋ช…์ด ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค..! ์ €๋„ ๋ณต์žกํ•ด์„œ ์™„์ „ ์ดํ•ด๋Š” ๋ชปํ–ˆ๋Š”๋ฐ,

  1. ์›๋ณธ ๋ฌธ์ž์—ด ์ฐธ์กฐํ•˜๊ณ 
  2. startIndex, endIndex ์Šฌ๋ผ์ด์Šคํ•˜๊ณ 
  3. ์Šฌ๋ผ์ด์Šคํ•œ ๋ฒ„ํผ๋ฅผ ์ƒˆ๋กœ ๋ณต์‚ฌํ•ด์„œ String์„ ๋งŒ๋“ ๋Œ€์šฉ

์ฒ˜์Œ์—” ์ œ๊ฐ€ ์‚ฌ์šฉํ•œ removeFirst()์ด ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ๋А๋ฆด ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์–ด์š”. String()๊นŒ์ง€ ๋ณต์žก๋„๋Š” ์ƒ๊ฐ์•ˆํ•ด์ฃผ๊ธฐ๋„ ํ–ˆ๊ณ ..
๋ฐฐ์—ด์ฒ˜๋Ÿผ ์•ž์—์„œ ๊ฐ’์„ ์ œ๊ฑฐํ•˜๋ฉด ๋’ค์— ์žˆ๋Š” ๋ชจ๋“  ์š”์†Œ๋ฅผ ํ•œ ์นธ์”ฉ ๋•ก๊ฒจ์•ผ ํ•˜๋‹ˆ๊นŒ ๋น„ํšจ์œจ์ ์ธ ๋ฐฉ์‹์ด์ž–์•„์š”? ๋’ค์—์„œ ๋นผ๋Š”๊ฒƒ๋ณด๋‹ค.. ๊ทธ๋ž˜์„œ ์ข€ ์•Œ์•„๋ณด๋‹ค๊ฐ€ COW๋ผ๋Š” ํ‚ค์›Œ๋“œ์— ๋Œ€ํ•ด์„œ ์กฐ๊ธˆ ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค..

Swift์˜ String (Array,Dictionary,Set๋„ ๊ฐ€๋Šฅ) ์€ ๋‚ด๋ถ€์ ์œผ๋กœ Copy-on-Write(COW)๊ฐ€ ์ ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•˜๋Š”๋ฐ์š”..! COW ๊ด€๋ จ ๋ธ”๋กœ๊ทธ
๊ทผ๋ฐ ํ•ด๋‹น ๋ฌธ์ž์—ด์˜ ์ฐธ์กฐ๊ฐ€ ์œ ์ผํ•˜๋‹ค๋ฉด, removeFirst()๋ฅผ ์จ๋„ COW๊ฐ€ ์ผ์–ด๋‚˜์ง€์•Š๊ณ , ๊ทธ๋ƒฅ ์Šฌ๋ผ์ด์Šค ์‹œ์ž‘ ํฌ์ธํ„ฐ๋งŒ ํ•œ ์นธ ์˜ฎ๊ธฐ๋Š” ์‹์œผ๋กœ ๋งค์šฐ ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋”๋ผ๊ณ ์š”.

์ด๋Ÿด ๋• ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(N)์ด ์•„๋‹ˆ๋ผ, ๊ฑฐ์˜ O(1)์— ๊ฐ€๊น๊ฒŒ ๋™์ž‘ํ•  ์ˆ˜๋„ ์žˆ๋‹ค๋„ค์š” ! (์‹ ๊ธฐ)
์š”์•ฝํ•˜๋ฉด ๋ณต์‚ฌํ•˜์ง€ ์•Š๋Š” ์ด์ƒ, ์ˆ˜์ •๊นŒ์ง€๋Š” ๋งค์šฐ ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌ๋  ์ˆ˜ ์žˆ๋‹ค์˜ ๋งฅ๋ฝ? ์ •๋„๋กœ ์ดํ•ดํ•ด์ฃผ์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”. <๊ทธ๋งŒํผ ๋ณต์‚ฌํ•˜๋Š”๋ฐ์— ๋ฆฌ์†Œ์Šค๊ฐ€ ๋œ๋“ ๋‹ค๋Š” ๊ฐœ๋…>

๋ฌผ๋ก  ๊ทธ๋ ‡๋‹ค๊ณ  ํ•ด์„œ COW๊ฐ€ ์•ˆ ์ผ์–ด๋‚œ๋‹ค -> ๋ฌด์กฐ๊ฑด ๋น ๋ฅด๋‹ค๋Š” ๊ฑด ๋˜ ์•„๋‹ˆ๋ผ๋„ค์š”...(์ž˜๋ชจ๋ฅด๋А ๋ถ„์•ผ๋ผ์„œ ๋งŽ์ด ํ—ท๊ฐˆ๋ฆฌ๋Š”๋ฐ ๋А๋‚Œ๋งŒ ๋ด์ฃผ์‹œ๊ธธ..)
COW๋กœ ์ƒˆ๋กœ ๋ณต์‚ฌ๋œ ๋ฒ„ํผ๊ฐ€ ์˜คํžˆ๋ ค ๋” ์บ์‹œ ์นœํ™”์ ์ธ ๊ตฌ์กฐ๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๊ณ ,
์ ‘๊ทผ ํŒจํ„ด์— ๋”ฐ๋ผ ์„ฑ๋Šฅ์ด ๋’ค์ง‘ํžˆ๋Š” ๊ฒฝ์šฐ๋„ ์กด์žฌํ•œ๋‹คํ•ฉ๋‹ˆ๋‹ท..

์ €๋„ ๋ธ”๋กœ๊ทธ์—ฌ๋Ÿฌ๊ฐœ๋ž‘ GPT์„ ์ƒ๋‹˜์ด๋ž‘ ์—ฌ๋Ÿฌ๊ฐœ ๋’ค์ง€๋ฉด์„œ ์ข€ ๋„์›€์ด ๋˜์—ˆ์œผ๋ฉดํ•ด์„œ ์จ๋ดค๋Š”๋ฐ ์ข€ ์ค‘๊ตฌ๋‚œ๋ฐฉ์ธ๊ฒƒ ๊ฐ™๋„ค์š”..
์ฒจ๋ถ€ํ•œ ๋งํฌ ๋ณด์‹œ๊ณ  ๋„์›€์ด ๋˜์—ˆ๋ฉดํ•ฉ๋‹ˆ๋‹ค..! ํ‹€๋ ธ๋‹ค๋ฉด ์ง€์ ๋„ ๋ถ€ํƒ๋“œ๋ ค์š”..!

์ถ”๊ฐ€

isKnownUniquelyReferenced
๋กœ ์ฐธ์กฐ๊ฐ€ ํ•˜๋‚˜์ธ์ง€ ์•Œ ์ˆ˜ ์žˆ์Œ

Copy link
Collaborator

@MuchanKim MuchanKim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด๋ฒˆ ์ฃผ์ฐจ๋ถ€ํ„ฐ๋Š” ํƒ€์ž„์–ดํƒ์œผ๋กœ ํ•œ๋ฒˆ ํ’€์–ด๋ณด๋ ค๊ณ ํ•ด์š”. ๊ธฐ์กด์— ๋„ค์ด๋ฐ์ด๋‚˜ ๋ชจ๋“ˆ๋กœ ๋”ฐ๋กœ ๋นผ๋Š”๊ฒŒ ์‹œ๊ฐ„์„ ๋” ์“ฐ๋Š” ๊ฒƒ ๊ฐ™๊ธฐ๋„ํ•ด์„œ.. ๋ฌดํŠผ ์ด ๋ฌธ์ œ๋Š” 20๋ถ„?์ •๋„ ๊ฑธ๋ฆฐ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ๋ถ„๋“ค ์ฝ”๋“œ๋ณด๋ฉด ์•„์ง Swift๋‹ต์ง€์•Š๊ฒŒ ํ‘ธ๋Š” ๊ฒƒ ๊ฐ™๋„ค์š”..ใ…Ž.ใ…Ž

์ „ 2์ฐจ์› ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ € 0..., 1... ์œผ๋กœ ์ธ๋ฑ์Šค๋ฅผ ์Šฌ๋ผ์ด์‹ฑ ํ•˜๋ ค๊ณ  ํ–ˆ๊ณ  ์ต์ˆ™ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ‘ผ ๊ฒƒ ๊ฐ™์•„์š”. ๋งˆ์นด๋กœ๋‹ˆ์”จ์˜ startIndex๊ฐ€ ์ €ํ•œํ…Œ j๋ผ๊ณ  ๋ณด๋ฉด ๋˜๊ณ , ์ €๋Š” j๋ถ€ํ„ฐ ์ˆœํšŒํ•ด์„œ ๋ฌธ์ž๋ฅผ ๋”ํ•ด์„œ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค์–ด ์คฌ์–ด์š”.

๊ฒฐ๋ก ์€.. ๋ฉ”๋ชจ๋ฆฌํšจ์œจ์ด๋‚˜ ์†๋„ ๋‘˜ ๋‹ค ์ƒ๊ฐํ•˜์ง€ ๋ชปํ•œ ์ฝ”๋“œ๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค.

+ ๊ธธ์ด forEach ์ž˜ ํ™œ์šฉํ•˜๊ธธ๋ž˜ ์ €๋„ ์จ๋ดค์Šต๋ฉ”๋‹ค.

import Foundation

// ๋ฌธ์ž์—ด ๋ฐฐ์—ด์œผ๋กœ ๋ฐ›๊ณ  ์•ž ๊ธ€์ž ํ•˜๋‚˜์”ฉ ๋‚ ๋ฆฌ๋ฉด์„œ ๋‹จ์–ด ๋งŒ๋“ค์–ด ์ฃผ๋ฉด ๋  ๋“ฏ.
// ๊ทธ๋ฆฌ๊ณ  ๋งŒ๋“  ๋‹จ์–ด ๋ฆฌ์ŠคํŠธ ์˜ค๋ฆ„์ฐจ์ˆœ sort

let n = Array(readLine()!.lowercased())
var res: [String] = []

for i in 0..<n.count {
    var word: String = ""
    
    for j in i..<n.count {
        word += String(n[j])
    }
    res.append(word)
}

res.sort(by: <)

res.forEach { i in print(i) }

Comment on lines +14 to +18
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ•€ ๋ง๋Œ€๋กœ swift๋‹ค์šด ๋А๋‚Œ์˜ ์ฝ”๋“œ์ธ ๊ฒƒ ๊ฐ™์•„์š”! ์‹œ์ž‘ ์ธ๋ฑ์Šค ์žก์•„์ฃผ๊ณ  ๋ฒ”์œ„์—ฐ์‚ฐ์ž ์จ์ฃผ๋Š”๊ฑฐ ์ข‹๋„ค์š”! ์ง๊ด€์ ์ด๋ฉด์„œ๋„ ๊ฐ„๊ฒฐํ•ฉ๋‹ˆ๋‹ค.
์นญ์ฐฌํ•ด ๋งˆ์นด๋กœ๋‹ˆ

์ „ ๋ฌธ์ž ์ˆœํšŒํ•˜๋ฉด์„œ ๋ฌธ์ž๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์คฌ๋Š”๋ฐ ๋น„ํšจ์œจ์ ์ธ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด๋“œ๋„ค์š”.. ์ฝ”๋“œ๋Š” ์ „์ฒด ๋ฆฌ๋ทฐ์— ๋‹ฌ๊ฒ ์Šต๋‹ˆ๋‹ค..!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forEach๋ฅผ ์“ฐ์‹œ๋‹ค๋‹ˆ ์—ญ์‹œ ๊ตฌํ˜„์™• ๋ฌด์ฐฌํ‚ด ๋ฌด์šฐ !!! sort(by:<) ์ง„์งœ {} () ์ด๋Ÿฐ์‹์œผ๋กœ ๋‚ด๋ถ€ ์กฐ๊ฑด๋“ค์„ ์†์‰ฝ๊ฒŒ ์Šค์œ„ํ”„ํ‹ฐํ•˜๊ฒŒ ๋ณ€๊ฒฝํ•˜๋˜๋ฐ ๋ฌด์šฐ์—๊ฒŒ ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ง„์งœ ๋ณ„๊ฑฐ ์•„๋‹Œ๋ฐ ์ €๋Š” sort๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์ด์ง€๋งŒ ํ•ญ์ƒ by:<๋กœ ์ผ๋˜ ๊ฒƒ ๊ฐ™์•„์š”. ๊ทผ๋ฐ ๋ถˆํ•„์š” ํ•œ ๊ฒƒ ๊ฐ™๋„ค์š”! ์ €๋„ ๊ฐ„๊ฒฐํ•˜๊ฒŒ sort()๋กœ ํ‘œํ˜„ ํ•ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.

res.sort(by: <)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"์˜คํžˆ๋ ค ์ข‹์•„"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"๊ทธ๋Ÿฐ๊ฐ€?"

Copy link
Collaborator

@YooGyeongMo YooGyeongMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํฌ๋กœ๋ฆฌ์•ผ ํฌ๋กœ๋ฆฌ์•ผ ์•„๋‹ˆ์•„๋‹ˆ ๊ธ€๋กœ๋ฆฌ์•ผ ๊ธ€๋กœ๋ฆฌ์•ผ ~~~ ์–ธ๋ƒ๋ž‘ ์ฝ”๋“œ ๊ฒ๋‚˜ ๋น„์Šท
m__842f974bcb18ef66632a9636e8bfe389184648__m

Comment on lines +14 to +25
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ธ€๋กœ๋‹ˆ ~~~~ ์š”!!

๋ฆฌ์–ผ๋ฃจ ์ •๋ง๋ฃจ ~ ์–ธ๋ƒ๋ž‘ ์ฝ”๋“œ๊ฐ€ ๋น„์Šทํ•œ๋ฐ ๋ญ ๊ฑฐ์ง„ ๋˜‘๊ฐ™๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๊ฒ ์œผ์˜ˆ

import Foundation

let str = readLine()!

var strArray: [String] = [str]

for i in 1..<str.count {
    let startIndex = str.index(str.startIndex, offsetBy: i)
    let substring = String(str[startIndex...])
    strArray.append(substring)
}

for item in strArray.sorted(){
    print(item)
}

๊ทธ๋Ÿฐ๋ฐ ๋ญ”๊ฐ€ ์ € String์œผ๋กœ ํ•œ๋ฒˆ ๋” ๋ณต์‚ฌํ•ด์ฃผ๋Š”๊ฒŒ ๋ฉ”๋ชจ๋ฆฌ๋ž‘ ์‹œ๊ฐ„ ๋ณต์žก๋„์ƒ ์ค‘์ฒฉ์ด ๋˜๋Š”๊ฑฐ ๊ฐ™์•„์„œ print(type(of:))๋ฅผ ๊ฒ๋‚˜ ์ฐ์–ด๋ณด๋‹ˆ

์—ญ์‹œ๋‚˜ str[startIndex...] ๋Š” Substring์ด๋ผ๋Š” ํƒ€์ž…์ด ์กด์žฌํ•˜๋”๊ตฐ์š” ! ๊ทธ๋ž˜์„œ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ถ€ํ„ฐ ๋‹ค ์ง‘์–ด๋„ฃ๋Š”๋‹ค ์ƒ๊ฐํ•˜๊ณ  ๋ฐฐ์—ด์„ ์• ์ดˆ์— Substring์œผ๋กœ ์„ ์–ธํ•˜๊ณ  ํ’€์–ด๋ณด๋‹ˆ ๋“ค์–ด๊ฐ€๋”๋ผ๊ตฌ์š” !!!

import Foundation

let str = readLine()!

var strArray: [Substring] = []

for i in 0..<str.count {
    let startIndex = str.index(str.startIndex, offsetBy: i)
    strArray.append(str[startIndex...])
//    print(type(of:str[startIndex...]))
}

for item in strArray.sorted(){
    print(item)
}

Xcode ์ž๋™์™„์„ฑ์“ฐ๋‹ˆ item ์ถ”์ฒœํ•ด์„œ ์ผ๋Š”๋ฐ ๊ธ€๋กœ๋‹ˆ๋„ ๊ทธ๋Ÿฐ๊ฑฐ์˜€๋‚˜์š”? ํฌํฌ ใ…‹ใ…‹

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://eunjin3786.tistory.com/458 substring ๊ด€๋ จ๊ธ€์ธ๋ฐ ์ฐธ๊ณ  ํ•˜์‹œ๋ฉด ์ข‹์„๋“ฏํ•ฉ๋‹ˆ๋‹ค ใ…Žใ…Ž

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print(type(of:) ์ฐ๋Š” ์Šต๊ด€ ์กด๊ฒฝํ•ฉ๋‹ˆ๋‹ค ๋ฐ๋ฏธ๊ฐ“..ใ…œใ…œ

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์งค ๋„ˆ๋ฌด ์›ƒ๊ฒจ์š” ใ…‹ใ…‹ใ…‹ใ…‹

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ ์•„๋‹ˆ ใ… ใ… ใ… 
๊ทผ๋ฐ ์ €๋Š” ๋ฌธ์ œ ํ’€๋ฉด์„œ ์ €๋ ‡๊ฒŒ ์‹œ๊ฐ„์ด ์–ผ๋งˆ๋‚˜ ๋ ๊นŒ .. ? ๋Š” ์ดˆ๊ณผ ๋์„ ๋•Œ๋‚˜ ํ™•์ธํ•˜๋Š”๋ฐ ์ง„์งœ ๋Œ€๋ฐ•์ 

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@giljihun @MuchanKim @sep037 ์š”์‹œ!! ์ด์ฟ ์ฃ  !!!

Copy link
Member

@giljihun giljihun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋Š” ์ผ๋‹จ ๊ฑ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋˜๋‚˜? ํ•˜๊ณ  ๋ฌด์ง€์„ฑ์œผ๋กœ ํ’€์–ด๋ณด๊ณ 
์ฝ”๋“œ๋ฅผ ์ค„์—ฌ๋ณด๋Š” ์—ฐ์Šต์„ ์ข€ ๋” ํ•ด๋ดค์Šต๋‹ˆ๋‹ค!

์ œ์ถœํ–ˆ๋˜์ฝ”๋“œ

/* MARK: - SOLUTION
 
 ๋ช…์‚ฌ์–ธ์–ดํ•™
 ํŒŒ์ƒ์–ด๋ฅผ ๋งŒ๋“œ๋Š” ํ˜•ํƒœ์†Œ์˜ ํ•˜๋‚˜. ์–ด๋–ค ๋‹จ์–ด์˜ ๋์— ๋ถ™์–ด ์ƒˆ๋กœ์šด ๋‹จ์–ด๊ฐ€ ๋˜๊ฒŒ ํ•˜๋Š” ๋ง. `๊ฐ€์œ„์งˆ'์˜ `-์งˆ' ๋”ฐ์œ„. ๋๊ฐ€์ง€. ๋’ท๊ฐ€์ง€. ๋ฐœ๊ฐ€์ง€. ์ ‘๋ฏธ์–ด. ใ€ˆ๋ถใ€‰ ๋’ค๋ถ™์ด. โ†”์ ‘๋‘์‚ฌ.
 
 -> ๋ญ์ง€ ์ด๊ฑฐ. '์ ‘๋ฏธ์‚ฌ'๋ผ๋Š” ํ‘œํ˜„์„ ์จ๋„ ๋˜๋Š” ๋ฌธ์  ๊ฐ€
 */

import Foundation

guard let S = readLine() else { exit(0) }

let chars = Array(S)
var result: [String] = []

for i in 0..<chars.count {
    var temp = ""
    for j in i..<chars.count {
        temp += String(chars[j])
    }
    result.append(temp)
}

print(result.sorted().joined(separator: "\n"))

dropFirst() ํ™œ์šฉํ•ด๋ณด๊ธฐ

import Foundation

guard let S = readLine() else { exit(0) }

let chars = Array(S)
var result: [String] = []

for i in 0..<S.count {
    result.append(String(S.dropFirst(i)))
}

print(result.sorted().joined(separator: "\n"))

๊ธฐ์กด ์ด์ค‘ํฌ๋ฌธ์€ ๋งค ๋ฃจํ”„๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ์ƒ์„ฑํ•˜๋Š”๋ฐ
dropFirst()๋ฅผ ์“ฐ๋‹ˆ String() ๋ณ€ํ™˜์„ ํ•  ๋•Œ๋งŒ ๋ณต์‚ฌ๊ฐ€ ๋ฐœ์ƒํ•ด์„œ ํ›จ์”ฌ ์„ฑ๋Šฅ์€ ์ข‹์„ ๊ฒƒ ๊ฐ™๊ณ  ์ง๊ด€์ ์ด์ฃ .

๋ฒˆ์™ธ1

import Foundation

guard let S = readLine() else { exit(0) }

let chars = Array(S)
var result: [String] = []

print((0..<S.count).map { String(S.dropFirst($0)) }.sorted().joined(separator: "\n"))

๋ฒˆ์™ธ2

import Foundation

guard let S = readLine() else { exit(0) }

let chars = Array(S)
var result: [String] = []

(0..<S.count).map { String(S.dropFirst($0)) }.sorted().forEach { print($0) }

์ด๊ฑด forEach๋กœ print๋ฅผ ๋ถˆ๋Ÿฌ์„œ .joined๋ฅผ ์•ˆ์“ฐ๋ฉด์„œ ๊ทนํ•œ์œผ๋กœ ์ค„์ด๊ธฐ ใ…Žใ…Ž

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swift์˜ String.index๋ฅผ ์ž˜ ํ™œ์šฉํ•œ ๋ฌธ์ œ๊ณ  ํ’€์ด๋„ ๊น”๋”ํ•˜๋„ค์—ด ~. ~

์‚ฌ์†Œํ•œ ๋ถ€๋ถ„์ธ๋ฐ, ์ €๋Š”

let num = N.count 

๋ถ€๋ถ„์ด ์กฐ๊ธˆ์€ ์ถ”์ƒ์ ์ด์ง€ ์•Š๋‚˜ ์‹ถ์Šต๋‹ˆ๋‹ค!
length, stringLength ์ด๋Ÿฐ๊ฑด ์–ด๋–จ๊นŒ์š”??

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var array: [String] = []

๋„ ๊ฒฐ๊ตญ ๋ฆฌํ„ด์šฉ์ด๋ผ๋ฉด result๋‚˜ answer๋„ ๊ดœ์ฐฎ์•„๋ณด์ด๊ตฌ์š”!
ํ˜น์€, ๊ตฌ์กฐ๋ฅผ ์„ค๋ช…ํ•œ๋‹ค๋ฉด suffixes?์ด๋Ÿฐ๊ฑฐ๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค~!!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ญ์•ผ ์ด ๋ฉ‹์Ÿ์ด๋Š”

Copy link
Collaborator

@YooGyeongMo YooGyeongMo May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@giljihun dropfirst(), ropfirst(),opfirst(),pfirst(),first(),irst(),rst(),st(),t()

Copy link
Member

@alstjr7437 alstjr7437 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์•„๋‹ˆ ์‰ฌ์šด ๋ฌธ์ œ์— ๋ญ์ด๋ฆฌ ์ฝ”๋ฉ˜ํŠธ๊ฐ€ ๋งŽ์ง€ ํ•˜๊ณ  ๋ณด๋‹ˆ๊นŒ
์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ธ๋ฑ์Šค์™€ removeFirst ๋“ฑ deep Dive์˜ ํ–ฅ์—ฐ์ด ์žˆ์—‡๊ตฐ์š”!!

๋งˆ์ง€๋ง‰์— ์ฝ์–ด์„œ ๋‹ค๋“ค ์žฌ๋ฐŒ์—ˆ์Šต๋‹ˆ๋‹ค!!

์ €๋Š” suffix๋กœ ๋ฌธ์ž๋ฅผ ๋ฝ‘์•„์คฌ์–ด์š”!

let S = readLine()!
var words: [String] = []

for i in 1...S.count {
    words.append(String(S.suffix(i)))
}

words.sort()

for word in words {
    print(word)
}

Comment on lines +15 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜คํ˜ธ ๋‹ค๋“ค ์ธ๋ฑ์Šค๋‚˜ dropFirst๋ฅผ ์‚ฌ์šฉํ•˜์…จ๊ตฐ์š”

์ €๋Š” suffix๋กœ ๋ฝ‘์•„๋‚ด์คฌ์Šต๋‹ˆ๋‹ค!

for i in 1...S.count {
    words.append(String(S.suffix(i)))
}

@sep037 sep037 merged commit 7f2a69b into main May 7, 2025
2 checks passed
@sep037 sep037 deleted the 3-sep037 branch May 11, 2025 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants