[Kotlin] ํ•ธ๋“œํฐ ๋ฒˆํ˜ธ ๊ฐ€๋ฆฌ๊ธฐ (ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜)
๋ฐ˜์‘ํ˜•

 

 

 

๋ฌธ์ œ

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

 

 


 

์ ‘๊ทผ

 

  • 1. phone_number๋ฅผ ํ•˜๋‚˜์”ฉ ์ž˜๋ผ์„œ ๋ฐฐ์—ด์— ๋„ฃ๋Š”๋‹ค.
  • 2. return ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด 0๋ถ€ํ„ฐ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ-5๋งŒํผ *์„ ๋„ฃ๋Š”๋‹ค.
  • 3. for๋ฌธ์„ ๋Œ๋ ค ๋ฐฐ์—ด์˜ ํฌ๊ธฐ -4๋ถ€ํ„ฐ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ -1๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋ฉฐ ์ถœ๋ ฅํ•œ๋‹ค.
  • 4. ํ•ด๋‹น ๋‚ด์šฉ์„ ๋ชจ๋‘ ๋ถ™์—ฌ String์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

 

// ๊ตฌ์ƒ, ๋Œ€๊ฐ• ์ด๋Ÿฐ ๋Š๋‚Œ์ด์ง€๋งŒ ๋ฌธ๋ฒ• ์˜ค๋ฅ˜ ๋‹ค์ˆ˜

class Solution {
    fun solution(phone_number: String): String {
        
     var numberArray = phone_number.split("")
     var returnArray = charArrayOf()
     for(i in numberArray.size-5) {
         var returnArray = returnArray + "*"}
     
     var f = numberArray.size-4
     for(f in numberArray.size-1) {
         returnArray += numberArray[f]}
     return returnArray.toString()
    }
}
  • ๋ฐฉ์‹์€ ๋งž์•˜์œผ๋‚˜, ๋ฌธ๋ฒ•์ด ์™•์ฐฝ ํ‹€๋ ธ๋‹ค. ์ด ์ฝ”๋“œ์˜ ๋ฌธ์ œ์ ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
  • 1. split ํ•จ์ˆ˜ ์ž˜๋ชป ์‚ฌ์šฉ
    split์€ ๋ฌธ์ž์—ด์„ ๋ถ„ํ• ํ•˜์—ฌ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ, ๋นˆ ๋ฌธ์ž์—ด ""์„ ๊ธฐ์ค€์œผ๋กœ ๋ถ„ํ• ํ•˜๋ฉด ๋ฌธ์ž์—ด์ด ๊ฐ ๋ฌธ์ž ๋‹จ์œ„๋กœ ๋ถ„ํ• ๋œ๋‹ค. ์ฆ‰, "1234567890"์ด๋ผ๋Š” ๋ฌธ์ž์—ด์„ split("")๋กœ ๋ถ„ํ• ํ•˜๋ฉด ๋นˆ ๋ฌธ์ž์—ด์„ ํฌํ•จํ•˜์—ฌ ["", "1", "2", ..., "9", "0"]๊ณผ ๊ฐ™์€ ๋ฐฐ์—ด์ด ์ƒ์„ฑ๋œ๋‹ค. ๋”ฐ๋ผ์„œ phone_number.split("")๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€์‹  phone_number.toCharArray()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • 2. for๋ฌธ ์ž˜๋ชป ์‚ฌ์šฉ
    for๋ฌธ์˜ ๊ธฐ๋ณธ์ ์ธ ๋ฌธ๋ฒ•์ด ํ‹€๋ ธ๋‹ค. ๋ฃจํ”„๋ฅผ ์ˆœํšŒํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” for (i in 0 until numberArray.size - 4) ๋˜๋Š” for (i in numberArray.size - 5 until numberArray.size - 1)๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, i in 0 until 10 ์˜ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.
    ๋˜ํ•œ CharArray์— *๋ฅผ ๋„ฃ๊ณ  ์‹ถ๋‹ค๋ฉด " "(๋”๋ธ”์ฟผํ„ฐ)๊ฐ€ ์•„๋‹ˆ๋ผ, ' '(์‹ฑ๊ธ€์ฟผํ„ฐ)์— ๋„ฃ์–ด์ค˜์•ผ ํ•œ๋‹ค.
  • 3. returnArray ๋ฐฐ์—ด ์ดˆ๊ธฐํ™” ์ž˜๋ชป ์‚ฌ์šฉ
    Kotlin์—์„œ ๋ฐฐ์—ด์„ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ๋Š” ๋นˆ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๊ณ  ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, arrayOf() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋นˆ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•ด์•ผ ํ•œ๋‹ค. (val returnArray = CharArray(numberArray.size))
  • 4. ๋ฆฌํ„ด๊ฐ’์—์„œ์˜ toString() ์ž˜๋ชป ์‚ฌ์šฉ
    returnArray.toString()์„ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์€ ์ž˜๋ชป๋๋‹ค. ์ด๊ฑด ๋ฐฐ์—ด์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ์€ ๋ฐฐ์—ด์˜ ๋‚ด์šฉ์ด ์•„๋‹ˆ๋ผ ๋ฐฐ์—ด์˜ ๋ฌธ์ž์—ด ํ‘œํ˜„์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋Œ€์‹  returnArray.joinToString("")๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๊ฒฐํ•ฉํ•œ๋‹ค.

 


 

์„ฑ๊ณต ์ฝ”๋“œ

class Solution {
    fun solution(phone_number: String): String {
        
     val numberArray = phone_number.toCharArray() 
     val returnArray = CharArray(numberArray.size) 
     for(i in 0 until numberArray.size - 4) { 
         returnArray[i] = '*' }
     
     for(i in numberArray.size-4 until numberArray.size)  { 
         returnArray[i] = numberArray[i]}
     return returnArray.joinToString("") 
    }
}

 


 

์„ค๋ช…

 

  1. ์ž…๋ ฅ๊ฐ’ phone_number๋ฅผ ํ•˜๋‚˜์”ฉ ์ž˜๋ผ์„œ ๋ฐฐ์—ด(numberArray)์— ๋„ฃ์Œ
  2. ์ •๋‹ต์„ ๋„ฃ์„ ๋ฐฐ์—ด(returnArray)์„ ๋งŒ๋“ค์–ด, numberArray์˜ ํฌ๊ธฐ๋งŒํผ ๋ฐฐ์—ด์„ ์ดˆ๊ธฐํ™”
  3. numberArray ๋ฐฐ์—ด์˜ ํฌ๊ธฐ - 5๊นŒ์ง€ * ์„ ์ •๋‹ต ๋ฐฐ์—ด(returnArray)์— ๋„ฃ์Œ
  4. numberArray ๋ฐฐ์—ด์˜ ํฌ๊ธฐ - 4๋ถ€ํ„ฐ -1๊นŒ์ง€์˜ ์›์†Œ ๊ทธ๋Œ€๋กœ ์ •๋‹ต ๋ฐฐ์—ด(returnArray)์— ๋„ฃ์Œ
  5. ์ •๋‹ต ๋ฐฐ์—ด(returnArray)์„ ๋ชจ๋‘ ๋ถ™์—ฌ String์œผ๋กœ ์ถœ๋ ฅ
  6. ๋‹ค๋งŒ ์ข€ ๋ณต์žกํ•˜๊ฒŒ ํ‘ผ ํŽธ์ด๋ผ, ์•„๋ž˜์˜ ๋‚ด์šฉ๋“ค์„ ์ฐธ๊ณ ํ•˜๋Š” ๊ฒŒ ์ข‹๋‹ค.

 


 

๋‹ค๋ฅธ ๋ฐฉ๋ฒ•

 

class Solution {
    fun solution(phone_number: String): String {
        return "${"".padStart(phone_number.length - 4, '*')}${phone_number.takeLast(4)}"
    }
}
  • padStart()์™€ takeLast()๋ฅผ ์ด์šฉํ•œ ๊ฐ„๋‹จํ•œ ํ’€์ด์ด๋‹ค.
  • ๐Ÿ’ก padStart(length: Int, padChar: Char): String
    - ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์— ์ง€์ •๋œ ๊ธธ์ด๋งŒํผ์˜ ๋ฌธ์ž(padChar)๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์ฃผ์–ด์ง„ ๊ธธ์ด์— ๋งž์ถ”๋Š” ์—ญํ• 
    - ๋นˆ ๋ฌธ์ž์—ด ""์€ ํŒจ๋”ฉ์ด ์ถ”๊ฐ€๋  ์œ„์น˜ (์–ด๋–ค ๋ฌธ์ž์—ด์ด ์˜ค๋”๋ผ๋„ ๊ฒฐ๊ณผ์—๋Š” ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๊ธฐ์— ""์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ์ผ๋ฐ˜์ )
    - "".padStart(phone_number.length - 4, '*') ๋ถ€๋ถ„์€ ''์„ ์ „ํ™”๋ฒˆํ˜ธ์˜ ๊ธธ์ด์—์„œ ๋งˆ์ง€๋ง‰ 4์ž๋ฆฌ๋ฅผ ์ œ์™ธํ•œ ๋งŒํผ ์ถ”๊ฐ€ํ•œ ๋ฌธ์ž์—ด์„ ์ƒ์„ฑ
  • ๐Ÿ’ก takeLast(n: Int): String
    - ๋ฌธ์ž์—ด์˜ ๋งˆ์ง€๋ง‰ n๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜
    - ์ด ์ฝ”๋“œ์—์„œ๋Š” phone_number.takeLast(4)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ „ํ™”๋ฒˆํ˜ธ์˜ ๋งˆ์ง€๋ง‰ 4์ž๋ฆฌ๋ฅผ ์ถ”์ถœ

 

 

 

class Solution {
    fun solution(phone_number: String): String {
        var answer = ""
        for((i, c) in phone_number.withIndex()) {
            if(phone_number.length - 4 <= i) {
                answer += c
            } else {
                answer += '*'
            }
        }
        return answer
    }
}
  • withIndex()๋ฅผ ์ด์šฉํ•œ ํ’€์ด์ด๋‹ค.
  • phone_number.withIndex()
    - ๋ฌธ์ž์—ด phone_number์˜ ๊ฐ ๋ฌธ์ž์™€ ํ•ด๋‹น ๋ฌธ์ž์˜ ์ธ๋ฑ์Šค๋ฅผ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ˜๋ณต์ž(iterator)๋ฅผ ๋ฐ˜ํ™˜
    - ์ด ๋ฐ˜๋ณต์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฌธ์ž(์—˜๋ฆฌ๋ฉ˜ํŠธ)์™€ ํ•ด๋‹น ์ธ๋ฑ์Šค๋ฅผ ๋™์‹œ์— ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ
  • for ((i, c) in phone_number.withIndex()) { ... }
    - phone_number์˜ ๊ฐ ๋ฌธ์ž(c)์™€ ํ•ด๋‹น ๋ฌธ์ž์˜ ์ธ๋ฑ์Šค(i)๋ฅผ ๊ฐ€์ ธ์™€์„œ ๋ฐ˜๋ณต์ ์œผ๋กœ ์ฒ˜๋ฆฌ
    - ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ „ํ™”๋ฒˆํ˜ธ์˜ ๊ฐ ๋ฌธ์ž์™€ ํ•ด๋‹น ์ธ๋ฑ์Šค์— ์ ‘๊ทผ

 

๐Ÿ’ก String์— ๋ฐ”๋กœ withIndex() ๋‚˜ lastIndex()๋ฅผ ์–ด๋–ป๊ฒŒ ์“ฐ๋Š” ๊ฑธ๊นŒ?

  • String์€ Array๋‚˜ List ํ˜•ํƒœ๊ฐ€ ์•„๋‹Œ๋ฐ ์™œ ๋ฐ”๋กœ ์ธ๋ฑ์Šค๊ฐ’์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š”์ง€ ๊ถ๊ธˆํ–ˆ๋‹ค.
  • ๋ฌธ์ž์—ด(String)์€ ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฌธ์ž(Char)์˜ ๋ฐฐ์—ด๋กœ ํ‘œํ˜„๋œ๋‹ค๊ณ  ํ•œ๋‹ค.
    ๋”ฐ๋ผ์„œ ๊ฐ ๋ฌธ์ž๋Š” ํ•ด๋‹น ๋ฌธ์ž์—ด์—์„œ์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ
  • ์ด๋Š” ์ฝ”ํ‹€๋ฆฐ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, Java, Python, C#, JS ๋ชจ๋‘ ๋™์ผํ•˜๋‹ค๊ณ  ํ•œ๋‹ค.
  • ๋”ฐ๋ผ์„œ, ๊ตณ์ด ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€ ์ด์œ ๊ฐ€ ์—†์—ˆ๋‹ค.

 

 

class Solution {
    fun solution(phone_number: String): String {
        var answer = ""
        val last = phone_number.lastIndex

        for (i in 0..last - 4) answer += "*"
        return answer + phone_number.slice(last - 3..last)
    }
}
  • .lastIndex๋ฅผ ์ด์šฉํ•ด ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋ฅผ ๊ตฌํ•˜๊ณ , ์ด๋ฅผ ์ด์šฉํ•ด ๋ฆฌํ„ดํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค.
  • phone_number.slice(last - 3..last)์—์„œ์˜ last - 3 .. last๋Š” ๋งˆ์ง€๋ง‰ 4์ž๋ฆฌ์˜ ์ธ๋ฑ์Šค ๋ฒ”์œ„๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค. 
  • slice(indices: IntRange): CharSequence
    - ๋ฌธ์ž์—ด์—์„œ ์ง€์ •๋œ ๋ฒ”์œ„์˜ ๋ฌธ์ž๋“ค์„ ์ถ”์ถœํ•˜์—ฌ ์ƒˆ๋กœ์šด CharSequence(์ผ๋ฐ˜์ ์œผ๋กœ String)๋กœ ๋ฐ˜ํ™˜
    - ์ธ๋ฑ์Šค ๋ฒ”์œ„๋Š” IntRange๋กœ ์ง€์ •
    - slice() ํ•จ์ˆ˜๋Š” ์›๋ณธ ์ปฌ๋ ‰์…˜์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  ์ƒˆ๋กœ์šด ์ปฌ๋ ‰์…˜์„ ๋ฐ˜ํ™˜ (์›๋ณธ ์ปฌ๋ ‰์…˜์€ ๊ทธ๋Œ€๋กœ ์œ ์ง€)

 

 

 

class Solution {
    fun solution(phone_number: String): String {
        var length = phone_number.length - 4
        var blind = ""
        for (i in 0 until length) {
            blind += "*"
        }
        return blind + phone_number.substring(length, length + 4)
    }
}
  • .substring()์„ ์ด์šฉํ•œ ํ’€์ด๋‹ค.
    - substring(startIndex: Int): ์‹œ์ž‘ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋ฌธ์ž์—ด์˜ ๋๊นŒ์ง€์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜
    - substring(startIndex: Int, endIndex: Int): ์‹œ์ž‘ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์ข…๋ฃŒ ์ธ๋ฑ์Šค ์ „๊นŒ์ง€์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ (until ์ฒ˜๋Ÿผ)
  • phone_number.substring(length, length + 4)
    - length๋ถ€ํ„ฐ length + 4๊นŒ์ง€์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์ถ”์ถœํ•˜์—ฌ ๋ฐ˜ํ™˜

 

 

 

 

๋ฐ˜์‘ํ˜•
 ๐Ÿ’ฌ C O M M E N T