[Swift - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์‹ ๊ทœ ์•„์ด๋”” ์ถ”์ฒœ

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

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


์นด์นด์˜ค ๊ณ„์ •๊ฐœ๋ฐœํŒ€์— ๋ฐฐ์น˜๋œ ์‹ ์ž…์‚ฌ์› ๋„ค์˜ค๊ฐ€ ์นด์นด์˜ค ์„œ๋น„์Šค์— ๊ฐ€์ž…ํ•˜๋Š” ์œ ์ €๋“ค์ด ๊ทœ์น™์— ๋งž์ง€ ์•Š๋Š” ์•„์ด๋””๋ฅผ ์ž…๋ ฅํ•˜์˜€์„ ๋•Œ, ๊ทœ์น™์— ๋งž์ถฐ ์ƒˆ๋กœ์šด ์•„์ด๋””๋ฅผ ์ถ”์ฒœํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“œ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์นด์นด์˜ค ์„œ๋น„์Šค ์•„์ด๋”” ๊ทœ์น™์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ์•„์ด๋””์˜ ๊ธธ์ด๋Š” 3์ž ์ด์ƒ 15์ž ์ดํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์•„์ด๋””๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž, ์ˆซ์ž, ๋นผ๊ธฐ(-), ๋ฐ‘์ค„(_), ๋งˆ์นจํ‘œ(.) ๋ฌธ์ž๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋‹จ, ๋งˆ์นจํ‘œ(.)๋Š” ์ฒ˜์Œ๊ณผ ๋์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ๋˜ํ•œ ์—ฐ์†์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ๊ทœ์น™์„ ๋งŒ์กฑํ•˜์ง€ ์•Š๋Š” ์•„์ด๋””๋Š” ์ด 7๊ฐ€์ง€์˜ ์ˆœ์ฐจ์ ์ธ ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์ณ ๊ฒ€์‚ฌํ•œ ํ›„ ์ƒˆ๋กœ์šด ์•„์ด๋””๋ฅผ ์ถ”์ฒœํ•ด ์ค„ ์ƒ๊ฐ์ž…๋‹ˆ๋‹ค.

  1. ๋ชจ๋“  ๋Œ€๋ฌธ์ž๋ฅผ ๋Œ€์‘๋˜๋Š” ์†Œ๋ฌธ์ž๋กœ ์น˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค
  2. ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž, ์ˆซ์ž, ๋นผ๊ธฐ(-), ๋ฐ‘์ค„(_), ๋งˆ์นจํ‘œ(.)๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  3. ๋งˆ์นจํ‘œ(.)๊ฐ€ 2๋ฒˆ ์ด์ƒ ์—ฐ์†๋œ ๋ถ€๋ถ„์„ ํ•˜๋‚˜์˜ ๋งˆ์นจํ‘œ(.)๋กœ ์น˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  4. ๋งˆ์นจํ‘œ(.)๊ฐ€ ์ฒ˜์Œ์ด๋‚˜ ๋์— ์œ„์น˜ํ•œ๋‹ค๋ฉด ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  5. ๋นˆ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด, "a"๋ฅผ ๋Œ€์ž…ํ•ฉ๋‹ˆ๋‹ค.
  6. ๊ธธ์ด๊ฐ€ 16์ž ์ด์ƒ์ด๋ฉด, ์ฒซ 15๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๋ฌธ์ž๋“ค์„ ๋ชจ๋‘ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ œ๊ฑฐ ํ›„ ๋งˆ์นจํ‘œ(.)๊ฐ€ ๋์— ์œ„์น˜ํ•œ๋‹ค๋ฉด ๋์— ์œ„์น˜ํ•œ ๋งˆ์นจํ‘œ(.) ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  7. ๊ธธ์ด๊ฐ€ 2์ž ์ดํ•˜๋ผ๋ฉด, ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ๊ธธ์ด๊ฐ€ 3์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์„œ ๋์— ๋ถ™์ž…๋‹ˆ๋‹ค.

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


import Foundation

func solution(_ new_id:String) -> String {
    let numeric: ClosedRange<Character> = "0"..."9"
    let alphabet: ClosedRange<Character> = "a"..."z"
    let symbols: [Character] = [".","-","_"]
    
    func filtering(_ c: Character) -> Bool {
        return numeric.contains(c) || alphabet.contains(c) || symbols.contains(c)
    }
    
    // ์†Œ๋ฌธ์ž๋กœ ์น˜ํ™˜, ๊ฐ€๋Šฅํ•œ ๋ฌธ์ž๋“ค๋งŒ ํ•„ํ„ฐ๋ง
    var recommended = new_id.lowercased().filter{filtering($0)}
    
    // "."๊ฐ€ ๋‘๊ฐœ ์ด์ƒ ์ด์–ด์ €์„œ ์กด์žฌํ•˜๋ฉด "."๋กœ ๋ณ€ํ™˜
    while recommended.contains("..") {
       recommended = recommended.replacingOccurrences(of: "..", with: ".")
    }
    
    // ์•„์ด๋””์˜ ์ฒ˜์Œ๊ณผ ๋์— "."๊ฐ€ ์กด์žฌํ•˜๋ฉด ์ œ๊ฑฐ
    while recommended.hasPrefix(".") {
        recommended.removeFirst()
    }
    
    while recommended.hasSuffix(".") {
        recommended.removeLast()
    }
    
    // ๋นˆ ๋ฌธ์ž์—ด์ผ ๊ฒฝ์šฐ "a"
    if recommended.isEmpty {
        recommended = "a"
    }
    
    // ์•„์ด๋””์˜ ๊ธธ์ด๊ฐ€ 16์ด์ƒ์ผ ๋•Œ ๋Š์–ด์„œ ์ฒ˜๋ฆฌ
    if recommended.count >= 16 {
        let temp = recommended.map{$0}
        if temp[14] == "." {
            recommended = String(temp[0...13])
        } else {
            recommended = String(temp[0...14])
        }
    }
    
    // ๊ธธ์ด๊ฐ€ 2์ดํ•˜์ผ ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ๊ธธ์ด๊ฐ€ 3์ด ๋  ๋•Œ๊นŒ์ง€ ์ด์–ด์„œ ๋ถ™ํžŒ๋‹ค.
    if recommended.count <= 2 {
        while recommended.count != 3 {
            recommended += String(recommended.last!)
        }
    }
    
    return recommended
}

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


๋ฌธ์ž์—ด์„ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ๋ฌผ์–ด๋ณด๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค. hasPrefix ๋ฉ”์„œ๋“œ์™€ hasSuffix ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋”์šฑ ์‰ฝ๊ฒŒ ์ ‘๊ทผ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

String ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ๋กœ์„œ ํ•ด๋‹น String์ด ์ „๋‹ฌ์ธ์ž๋กœ ์ฃผ์–ด์ง„ String์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€, ๋๋‚˜๋Š”์ง€๋ฅผ Bool ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๋“ค์ž…๋‹ˆ๋‹ค. ์ด ๋‘ ํ•จ์ˆ˜ ๋ชจ๋‘ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜๋‹ˆ ์‚ฌ์šฉํ•  ๋•Œ ์ฃผ์˜ํ•˜๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

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


 

Apple Developer Documentation

 

developer.apple.com