๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/Kotlin

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค • ์ฝ”ํ‹€๋ฆฐ] ์‹œ์ € ์•”ํ˜ธ #12926

by ํ‚ค์œค 2023. 11. 28.

#12926

๐ŸŽ„ Question ?

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

class Solution {
    fun solution(s: String, n: Int): String {
        var answer = ""
        return answer
    }
}

๐Ÿงฉ Thought Process

  1. ์•„์Šคํ‚ค ์ฝ”๋“œ ์‘์šฉํ•˜๊ธฐ
  2. string์„ toInt()๋กœ ๋ฐ”๊พธ์–ด์ฃผ๋ฉด ์•„์Šคํ‚ค์ฝ”๋“œ ๊ฐ’์„ ๋ฐ›๋Š”๋‹ค.  ๊ทธ ์•„์Šคํ‚ค ์ฝ”๋“œ ๊ฐ’์—๋‹ค๊ฐ€ n์„ ๋”ํ•˜๊ณ  ๋‹ค์‹œ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ์–ด์ค€๋‹ค. toString() ์‚ฌ์šฉ
  3. ์—ฌ๊ธฐ์— if๋ฌธ์„ ์ถ”๊ฐ€ํ•˜์—ฌ string์„ toInt()๋กœ๋ฐ”๊พธ๊ณ  n์„ ๋”ํ–ˆ์„ ๋•Œ
    ๋Œ€๋ฌธ์ž์ด๋ฉด: 90๋ณด๋‹ค ํฌ๋ฉด ์ดˆ๊ณผ๋œ ๊ฐ’๋งŒํผ 65์—๋‹ค๊ฐ€ ๋”ํ•ด์ค€๋‹ค.
    ์†Œ๋ฌธ์ž์ด๋ฉด: 122๋ณด๋‹ค ํฌ๋ฉด ์ดˆ๊ณผ๋œ ๊ฐ’๋งŒํผ 97์—๋‹ค๊ฐ€ ๋”ํ•ด์ค€๋‹ค.
  4. ๋ฆฌ์ŠคํŠธ์— ์ŠคํŽ˜์ด์Šค๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ์•„์Šคํ‚ค์ฝ”๋“œ ๊ฐ’์ด 32์ด๋ฏ€๋กœ ์ด ๊ฒฝ์šฐ๋Š” ์ œ์™ธ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋„๋ก ์ฝ”๋“œ๋ฅผ ์งœ์คฌ๋‹ค.

 

๐ŸŽ€ Answer

class Solution {
    fun solution(s: String, n: Int): String {
        var answer = 0
        var sChar = s.toCharArray()
        for (i in s.indices) {
            var sInt = sChar[i].toInt()
            println(sInt)
            if (sInt == 32) {
                continue
            } else if (sInt < 91) {
                sInt += n
                if (sInt > 90) {
                    sInt -= 26
                }
            } else if (sInt > 96) {
                sInt += n
                if (sInt > 122) {
                    sInt -= 26
                }
            }
            sChar.set(i, sInt.toChar())
        }
        
        return sChar.joinToString("")
    }
}

๋‹ค๋ฅธ ํ’€์ด

class Solution {
    public String solution(String s, int n) {
        StringBuilder answer = new StringBuilder();
        
        for(char c : s.toCharArray()){
            if(c >= 'a' && c <= 'z'){
                answer.append((char)(c + n - (c + n <= 'z' ? 0 : 26)));
            }else if(c >= 'A' && c <= 'Z'){
                answer.append((char)(c + n - (c + n <= 'Z' ? 0 : 26)));
            }else{
                answer.append(c);
            }
        }
        
        return answer.toString();
    }
}
์ถœ์ฒ˜: https://yline.tistory.com/70 [Y_LINE's_Repository:ํ‹ฐ์Šคํ† ๋ฆฌ]

SpringBuilder:

https://developer.android.com/reference/kotlin/java/lang/StringBuilder

class Solution {
    fun solution(s: String, n: Int): String {
        return s.toCharArray().map{
            when(it){
                in 'A'..'Z' -> {
                    if(it + n > 'Z') it + n - 26 else it + n
                }in 'a'..'z' -> {
                    if(it + n > 'z') it + n - 26 else it + n
                }else -> {
                    it
                }
            }
        }.joinToString("")
    }
}
์ถœ์ฒ˜: https://yline.tistory.com/70 [Y_LINE's_Repository:ํ‹ฐ์Šคํ† ๋ฆฌ]

 ์ด๊ฑด ๋‚ด๊ฐ€ ํ•œ ํ’€์ด๋ž‘ ๊ฐ€๊นŒ์šด๋“ฏ ๊ทธ๋Ÿฐ๋ฐ when ์ด๋ž‘ in ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ํŠน์ดํ•˜๋‹ค.

๐ŸŽ Result

๐Ÿ† Comment

์ง์ ‘ ํ’€์–ด์„œ ๋ฟŒ๋“ฏ.