[Swift - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] [3์ฐจ] ํŒŒ์ผ๋ช… ์ •๋ ฌ

2021. 5. 25. 18:04ใ†๐Ÿง‘๐Ÿป‍๐Ÿ’ป iOS ๊ฐœ๋ฐœ/Swift - Algorithm - Solutions

๐Ÿ•ต๐Ÿป  ๋ฌธ์ œ ํ•ด์„


ํŒŒ์ผ ์ €์žฅ์†Œ ์„œ๋ฒ„ ๊ด€๋ฆฌ๋ฅผ ๋งก๊ฒŒ ๋œ ๋ฌด์ง€๊ฐ€ ๊ธฐ์กด์— ์ด๋ฆ„ ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ํŒŒ์ผ๋“ค์„ ๋‹ค์‹œ ๋ณด๊ธฐ ์ข‹๊ฒŒ ์ •๋ ฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ๋ช…์€ ํฌ๊ฒŒ 3 ๊ฐ€์ง€๋กœ ๊ตฌ์„ฑ๋˜๋Š”๋ฐ HEAD, NUMBER, TAIL์ž…๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ํŠน์ง•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

ํŒŒ์ผ๋ช…์—์„œ ๋จผ์ € HEAD๊ฐ€ ๋“ฑ์žฅํ•˜๋ฉฐ ๊ทธ ์ดํ›„ 0์—์„œ ์ตœ๋Œ€ 99999๊นŒ์ง€์˜ NUMBER๊ฐ€ ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค. ์ดํ›„ TAIL์ด ๋“ฑ์žฅํ•˜์ง€๋งŒ ์ •๋ ฌ๊ธฐ์ค€์—๋Š” ์กด์žฌํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๊ฐํ•˜์ง€ ์•Š๊ฒ ์Šต๋‹ˆ๋‹ค. ์ •๋ ฌ ์šฐ์„ ์ˆœ์œ„๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • HEAD ๊ธฐ์ค€์œผ๋กœ ์‚ฌ์ „์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค. ์ด๋•Œ ๋Œ€์†Œ๋ฌธ์ž๋Š” ๊ตฌ๋ณ„ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • HEAD๊ฐ€ ๋™์ผํ•  ๊ฒฝ์šฐ, NUMBER๋ฅผ ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. 
  • HEAD์™€ NUMBER๊ฐ€ ๋ชจ๋‘ ๋™์ผํ•˜๋‹ค๋ฉด, ์ž…๋ ฅ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ํŒŒ์ผ๋ช…์ด ์ฃผ์–ด์ง€๋ฉด HEAD์™€ NUMBER ๊ทธ๋ฆฌ๊ณ  ์ž…๋ ฅ ์ˆœ์„œ ์ด ์„ธ ๊ฐ€์ง€๋ฅผ ๋ฝ‘์•„๋‚ด์„œ ๋ฐฐ์—ด์— ์ €์žฅํ•œ ํ›„, ์ •๋ ฌ ๊ทœ์น™์— ๋งž๊ฒŒ ์ •๋ ฌํ•˜์—ฌ ๋ฆฌํ„ดํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋จผ์ € ํŒŒ์ผ๋ช…์„ String ํƒ€์ž… ๋ฉ”์„œ๋“œ์ธ .components๋ฅผ ์ด์šฉํ•˜์—ฌ ์ˆซ์ž๋ฅผ ์ œ์™ธํ•œ ๋ฌธ์ž์—ด๋“ค๋กœ ๋‚˜๋ˆ„์–ด ์ฃผ๊ณ  ๊ทธ ์ฒซ ๋ฒˆ์งธ ์›์†Œ๋ฅผ HEAD๋กœ ์ถ”์ถœํ–ˆ์Šต๋‹ˆ๋‹ค.

let numbers: CharacterSet = ["0","1","2","3","4","5","6","7","8","9"]

for idx in 0..<files.count {
        let fileComponents =  files[idx].components(separatedBy: numbers)
        var temp = [String]()
        
        for component in fileComponents {
            if component != "" {
                temp.append(component)
            }
        }
        
        let head = temp[0]
}

๊ทธ ํ›„, ๋ณธ๋ž˜ ํŒŒ์ผ๋ช…์—์„œ HEAD๋ฅผ ์ œ๊ฑฐํ•˜๋ฉด ๋‹ค์‹œ ๋ฌธ์ž๊ฐ€ ๋“ฑ์žฅํ•˜๊ธฐ ์ „๊นŒ์ง€ ์ˆซ์ž๋“ค์„ NUMBER๋ผ๊ณ  ํŒ๋‹จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. String ํƒ€์ž…๋ฉ”์„œ๋“œ์ค‘ .replacingOccurrences() ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ HEAD๋ฅผ ์ œ๊ฑฐํ•œ ํ›„, ์ˆซ์ž๋ฅผ ๊ตฌํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. 

๊ทธ ํ›„, HEAD์˜ ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ณ„์„ ์—†์• ๊ธฐ์œ„ํ•ด, ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ์•ž์„œ ๊ตฌํ•œ "00100"์ฒ˜๋Ÿผ ์•ž์— 0์„ ํฌํ•จํ•œ ์ˆซ์ž๋“ค์€ Int() ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด 0์„ ์ œ๊ฑฐํ•œ Int ๊ฐ’์œผ๋กœ NUMBER๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ (String, Int, Int)  ํŠœํ”Œ์— ์ €์žฅํ•˜๊ณ  ๋ฐฐ์—ด์— ๋‹ด์•„ ์ •๋ ฌํ•˜์—ฌ ์ •๋‹ต์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“ฆ  ๋ฌธ์ œ ํ’€์ด & ์ฝ”๋“œ


func solution(_ files:[String]) -> [String] {
    let numbers: CharacterSet = ["0","1","2","3","4","5","6","7","8","9"]
    let numbersString: [Character] = ["0","1","2","3","4","5","6","7","8","9"]
    var headNumberInputOrder = [(head: String, number: Int, idx: Int)]()
    var answer = [String]()
    
    for idx in 0..<files.count {
    
        // file[idx] = "foo010bar020.zip"
        let fileComponents =  files[idx].components(separatedBy: numbers)
        var temp = [String]()
        
        // fileComponents = ["foo","" ,"" ,"" ,"bar" ,"" ,"" ,"" ,".zip"]
        for component in fileComponents {
            if component != "" {
                temp.append(component)
            }
        }
        // temp = ["foo", "bar", ".zip"]
        
        let head = temp[0]
        
        let removeHead = files[idx].replacingOccurrences(of: head, with: "")
        // removeHead = "010bar020.zip"
        var number = ""
        
        for nums in removeHead {
            if numbersString.contains(nums) {
                number += String(nums)
            } else {
                break
            }
        }
        
        // ("FOO", 10, 0) ๋กœ ์ €์žฅ (๋Œ€๋ฌธ์žํ™”ํ•œ HEAD, NUMBER, ์ž…๋ ฅ์ˆœ์„œ)
        
        headNumberInputOrder.append((head.uppercased(), Int(number)!, idx))
    }
    
    // ๊ธฐ์ค€์— ๋งž๊ฒŒ ์ •๋ ฌ
    
    headNumberInputOrder.sort {
        if $0.head == $1.head {
            if $0.number == $1.number {
                return $0.idx < $1.idx
            }
            return $0.number < $1.number
        }
        return $0.head < $1.head
    }
    
    // ๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•˜๋ฉฐ answer์— ์ •๋ ฌ ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅ
    
    for tuple in headNumberInputOrder {
        answer.append(files[tuple.2])
    }
    
    return answer
}

 

๐Ÿคท๐Ÿป‍โ™‚๏ธ  ๋” ์ƒ๊ฐํ•ด๋ณด๊ธฐ


์ œ ํ’€์ด๋Š” ์ข€ ์ง€์ €๋ถ„ ํ•˜๊ฒŒ HEAD์™€ NUMBER๋ฅผ ๊ตฌํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ถ„๋“ค์˜ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ•˜๋‹ˆ ํ›จ์”ฌ ๋” ์‰ฝ๊ณ  ๊ฐ„ํŽธํ•œ ํ’€์ด ๋ฒ•๋“ค์ด ์žˆ๋”๊ตฐ์š”. ์•„์ง ๊ฐˆ ๊ธธ์ด ๋จผ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.. ํฌ๊ฒŒ HEAD์™€ NUMBER๋ฅผ ๊ตฌํ•  ๋•Œ ์ •๊ทœ์‹์„ ์ด์šฉํ•˜์—ฌ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ drop๊ณผ prefix๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์–ด์„œ ๊ฐ„๋žตํ•˜๊ฒŒ ์ •๋ฆฌ๋ฅผ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

String.range(of: "์ •๊ทœ ํ‘œํ˜„์‹", option: [.regularExpression]) 

์œ„์˜ String์˜ ํƒ€์ž…๋ฉ”์„œ๋“œ๋Š” of: ์— ํ•ด๋‹นํ•˜๋Š” ์ •๊ทœ์‹์— ๋งž๋Š” ๋ฒ”์œ„๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ •๊ทœ ํ‘œํ˜„์‹์— ํ•ด๋‹นํ•˜๋Š” ๊ฒƒ์ด ์—†๋‹ค๋ฉด nil์„ ๋ฆฌํ„ดํ•˜๋ฏ€๋กœ if let ๊ตฌ๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ๋ฒ”์œ„๋ฅผ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํŒŒ์ผ๋ช…์˜ HEAD์˜ ๋ฒ”์œ„๋Š” ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ์ˆ˜๊ฐ€ ๋“ฑ์žฅํ•  ๋•Œ๊นŒ์ง€์˜ ๋ฒ”์œ„๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 

๊ทธ๋Ÿผ HEAD๋ถ€๋ถ„์„ ์ œ์™ธํ•˜๊ณ  ์ˆซ์ž๊ฐ€ ๋“ฑ์žฅํ•˜๋Š” ๋ถ€๋ถ„์˜ ๋ฒ”์œ„๋ฅผ ๊ตฌํ•˜์—ฌ NUMBER๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ •๊ทœ ํ‘œํ˜„์‹์€ ๊ตฌ๊ธ€์— ๊ฒ€์ƒ‰ํ•˜๋ฉด ์ž์„ธํ•˜๊ฒŒ ์„ค๋ช…์ด ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ์›Œ๋‚™ ์–‘์ด ๋งŽ๊ณ  ๋ฐฉ๋Œ€ํ•ด์„œ ํ•ด๋‹น ๊ธ€์—์„  ์„ค๋ช…ํ•˜์ง€ ๋ชปํ•˜์ง€๋งŒ ์ฐธ๊ณ ํ•˜์…”์„œ ์ตํ˜€๋‘์‹œ๋ฉด ๋ฌธ์ž์—ด์„ ์กฐ์ž‘ํ•˜๋Š” ๋ฌธ์ œ์—์„œ ๋”์šฑ ๊ฐ„๋‹จํ•œ ํ•ด๊ฒฐ๋ฒ•์„ ์ฐพ์„ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

String.drop()  -> drop์•ˆ์˜ ์กฐ๊ฑด์‹์„ ๋งŒ์กฑํ•˜๋Š” ๋ถ€๋ถ„์„ ์‚ญ์ œํ•˜๊ณ  ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
String.prefix() -> ์กฐ๊ฑด์‹์„ ๋งŒ์กฑํ•˜๋Š” ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์€ Range, drop๊ณผ prefix๋ฅผ ์ด์šฉํ•˜์—ฌ HEAD์™€ NUMBER๋ฅผ ๊ตฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

prefix๋ฅผ ์ด์šฉํ•˜์—ฌ ์ˆซ์ž๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๋Š” ๋ถ€๋ถ„์„ ๊ตฌํ•˜์—ฌ HEAD๋ฅผ ๊ตฌํ•˜๊ณ  , drop์„ ์ด์šฉํ•˜์—ฌ HEAD๋ฅผ ์ง€์šฐ๊ณ  ๋‹ค์‹œ prefix๋ฅผ ์ด์šฉํ•˜์—ฌ NUMBER๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. 

๋‚˜๋จธ์ง€ ๊ณผ์ •์€ ๋˜‘๊ฐ™์ด (HEAD, NUMBER, ์ž…๋ ฅ์ˆœ์„œ) ์ด ์„ธ ๊ฐ€์ง€๋ฅผ ๋น„๊ตํ•˜์—ฌ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•ด๊ฒฐํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.


๐Ÿ‘๐Ÿป  ์ฐธ๊ณ 

 

[Swift] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Lv2) - [3์ฐจ] ํŒŒ์ผ๋ช… ์ •๋ ฌ (2018 KAKAO BLIND RECRUITMENT )

์•ˆ๋…•ํ•˜์„ธ์š” \~~ ๋ฒจ๋กœ๊ทธ์˜ ์ฒซ ๊ธ€์€ ์˜ค๋Š˜ ํ‘ผ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”ํ…Œ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.https://programmers.co.kr/learn/courses/30/lessons/17686swift์—” numeric option์ด ์žˆ๋Š”๋ฐ ๊ทธ๊ฑธ๋กœ ํ’€๊ณ  ์‹ถ์–ด์„œ ๋ถ™์žก๊ณ  ์žˆ๋‹ค๊ฐ€ nu

velog.io

  • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๋‹˜์˜ ํ’€์ด