๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
3๏ธโƒฃ Swift/Problem Solving

[Swift Algorithm] ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ ๊ฐ€๋ฆฌ๊ธฐ Programmers(Lv.1)

by seolhee2750 2021. 6. 6.
๋ฌธ์ œ ์„ค๋ช…

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ชจ๋ฐ”์ผ์€ ๊ฐœ์ธ์ •๋ณด ๋ณดํ˜ธ๋ฅผ ์œ„ํ•ด ๊ณ ์ง€์„œ๋ฅผ ๋ณด๋‚ผ ๋•Œ ๊ณ ๊ฐ๋“ค์˜ ์ „ํ™”๋ฒˆํ˜ธ์˜ ์ผ๋ถ€๋ฅผ ๊ฐ€๋ฆฝ๋‹ˆ๋‹ค.
์ „ํ™”๋ฒˆํ˜ธ๊ฐ€ ๋ฌธ์ž์—ด phone_number๋กœ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ „ํ™”๋ฒˆํ˜ธ์˜ ๋’ท 4์ž๋ฆฌ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ˆซ์ž๋ฅผ ์ „๋ถ€ *์œผ๋กœ ๊ฐ€๋ฆฐ ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ ์กฐ๊ฑด
  • s๋Š” ๊ธธ์ด 4 ์ด์ƒ, 20 ์ดํ•˜์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ
phone_number return
"01033334444" "*******4444"
"027778888" "*****8888"

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
func solution(_ phone_number:String) -> String {
    var arr = [String]()
    var phoneNumCount = phone_number.count
    
    while (phoneNumCount - 4 > 0){
        arr.append("*")
        phoneNumCount -= 1
    }
    
    let result:String = arr.joined() + phone_number.suffix(4)
    
    return result
}
  • *๋ฅผ ์ฐ๊ธฐ ์œ„ํ•œ arr ๋ฐฐ์—ด ์ƒ์„ฑ
  • count ์ด์šฉํ•˜์—ฌ ์ž…๋ ฅ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด ์ €์žฅ
  • ์›๋ž˜๋Œ€๋กœ ์ˆซ์ž๋ฅผ ๋‚จ๊ฒจ๋‘ฌ์•ผ ํ•˜๋Š” ๋งจ ๋’ค 4์ž๋ฆฌ๋ฅผ ์ œ์™ธํ•˜๊ณ ,
    ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์˜ ํฌ๊ธฐ ๋งŒํผ arr ๋ฐฐ์—ด์— *๋ฅผ append ์ด์šฉํ•˜์—ฌ ์ถ”๊ฐ€ํ•˜๋Š” while๋ฌธ ์‚ฌ์šฉ
  • *๋ฅผ ์ฐ์–ด์ค€ arr ๋ฐฐ์—ด์„ joined์„ ์‚ฌ์šฉํ•ด String์œผ๋กœ ํ•ฉ์ณ์ฃผ๊ณ ,
    ๊ฑฐ๊ธฐ์— suffix๋ฅผ ์ด์šฉํ•˜์—ฌ ์ฒ˜์Œ ์ž…๋ ฅ ๋ฐ›์•˜๋˜ ๋ฌธ์ž์—ด์˜ ๋งจ ๋’ค 4๊ธ€์ž๋ฅผ ๋ถ™์—ฌ์คŒ

 

์ธ์ƒ์ ์ธ ํ’€์ด
func solution(_ phone_number:String) -> String {
    return String("\(String(repeating: "*", count: phone_number.count - 4))\(phone_number.suffix(4))")
}
  • repeating์„ ์ด์šฉํ•˜์—ฌ *์„ ์›ํ•˜๋Š” ๋งŒํผ ์ฐ์–ด์ฃผ์—ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ๊ฐ™์€ ๋ฌธ์ž ๋ฐ˜๋ณต ์ถœ๋ ฅ์„ ์œ„ํ•ด์„œ๋Š” repeating์„ ์• ์šฉํ•˜์ž!
  • repeating ์‚ฌ์šฉํ•˜๋‹ˆ while๋ฌธ์ด ํ•„์š”์—†์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, joined๋„ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†์–ด์ง„๋‹ค.
  • suffix ํ•จ์ˆ˜์˜ ์กด์žฌ์— ๋Œ€ํ•ด์„œ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. suffix, prefix ๊ฐ™์€ ํ•จ์ˆ˜๋“ค์˜ ๊ฐœ๋…์„ ์ •๋ฆฌํ•ด๋ด์•ผ๊ฒ ๋‹ค.

 


 

๐Ÿ– [ ์ถ”๊ฐ€ ] 1์ฃผ์ผ ํ›„ ๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ

func solution(_ phone_number:String) -> String {
    var result = [String]()
    
    result.append(String(repeating: "*", count: phone_number.count-4))
    return result.joined() + phone_number.suffix(4)
}
  • repeating ์‚ฌ์šฉํ•˜์—ฌ *๋ฅผ ์ž˜ ์ฐ์–ด์ฃผ์—ˆ๊ณ , suffix ์‚ฌ์šฉํ•˜์—ฌ ๋งจ ๋’ค 4์ž๋ฆฌ ์ˆ˜๋ฅผ ๊ตฌํ•ด์ฃผ์—ˆ๋‹ค.
  • joined๋กœ * ์ฐ์€ ๋ฐฐ์—ด์„ ์ถœ๋ ฅํ•ด์ฃผ์—ˆ๋Š”๋ฐ, ์œ„์˜ ์ธ์ƒ์ ์ธ ํ’€์ด์ฒ˜๋Ÿผ ๊ทธ๋ƒฅ String์œผ๋กœ ๋ฌถ์–ด ์ถœ๋ ฅํ•ด์ค˜๋„ ๋๊ฒ ๋‹ค.
  • ์ฒซ ๋ฒˆ์งธ ํ’€์ด๋ณด๋‹ค ํ›จ์”ฌ ๊ฐ„๊ฒฐํ•ด์กŒ๊ณ , ๊ฐ€๋…์„ฑ๋„ ์ข‹์•„์ง„ ๋“ฏ ํ•˜๋‹ค!

 


 

โž•๐Ÿ– [ ๋˜! ์ถ”๊ฐ€ ] 1์ฃผ์ผ ํ›„  ๋˜ ๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ

func solution3(_ phone_number:String) -> String {
    var arr = Array(phone_number)
    for i in 0..<phone_number.count-4 { arr[i] = "*" }
    return String(arr)
}
  • suffix๊ฐ™์€ ํ•จ์ˆ˜๋ฅผ ๋”ฐ๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ํ’€์—ˆ๋‹ค.
  • ์ฃผ์–ด์ง„ ์ „ํ™”๋ฒˆํ˜ธ์™€ ๊ฐ™์€ var ๋ฐฐ์—ด์„ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ , ๋’ค 4๊ธ€์ž๋ฅผ ์ œ์™ธํ•œ ๊ธ€์ž๋“ค๋งŒ *๋กœ ์น˜ํ™˜ํ•ด์ฃผ์—ˆ๋‹ค.
  • ๊ตณ์ด ํ•„์š”ํ•˜์ง€ ์•Š์€ ํ•จ์ˆ˜๋ฅผ ์ œ์™ธํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋‹ˆ ๋งˆ์Œ์ด ํŽธ์•ˆํ•ด์ง„๋‹ค,,

 


 

๋ฌธ์ œ

https://programmers.co.kr/learn/courses/30/lessons/12948

 

๋Œ“๊ธ€