#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
- ์์คํค ์ฝ๋ ์์ฉํ๊ธฐ
- string์ toInt()๋ก ๋ฐ๊พธ์ด์ฃผ๋ฉด ์์คํค์ฝ๋ ๊ฐ์ ๋ฐ๋๋ค. ๊ทธ ์์คํค ์ฝ๋ ๊ฐ์๋ค๊ฐ n์ ๋ํ๊ณ ๋ค์ ๋ฌธ์์ด๋ก ๋ฐ๊พธ์ด์ค๋ค. toString() ์ฌ์ฉ
- ์ฌ๊ธฐ์ if๋ฌธ์ ์ถ๊ฐํ์ฌ string์ toInt()๋ก๋ฐ๊พธ๊ณ n์ ๋ํ์ ๋
๋๋ฌธ์์ด๋ฉด: 90๋ณด๋ค ํฌ๋ฉด ์ด๊ณผ๋ ๊ฐ๋งํผ 65์๋ค๊ฐ ๋ํด์ค๋ค.
์๋ฌธ์์ด๋ฉด: 122๋ณด๋ค ํฌ๋ฉด ์ด๊ณผ๋ ๊ฐ๋งํผ 97์๋ค๊ฐ ๋ํด์ค๋ค. - ๋ฆฌ์คํธ์ ์คํ์ด์ค๊ฐ ์๋ ๊ฒฝ์ฐ์๋ ์์คํค์ฝ๋ ๊ฐ์ด 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
์ง์ ํ์ด์ ๋ฟ๋ฏ.