[Programmers] #120892 - 암호 해독 [Java][C++][Python]
[Programmers] #120892 - 암호 해독 [Java][C++][Python]
1. 아이디어
암호문에서 $\text{code}$의 배수 번째 글자만 모으는 문제다. 번째는 1부터 세므로 0부터 세는 인덱스로는 $\text{code} - 1$이 첫 글자이고, 이후 $\text{code}$씩 건너뛴 위치가 차례로 해당한다.
2. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
10
class Solution {
public String solution(String cipher, int code) {
StringBuilder sb = new StringBuilder();
for (int i = code - 1; i < cipher.length(); i += code) {
sb.append(cipher.charAt(i));
}
return sb.toString();
}
}
1
2
3
4
5
6
7
8
9
10
11
#include <bits/stdc++.h>
using namespace std;
string solution(string cipher, int code) {
string s;
for (int i = code - 1; i < cipher.size(); i += code) {
s += cipher[i];
}
return s;
}
1
2
def solution(cipher, code):
return cipher[code - 1 :: code]
슬라이스의 시작과 간격으로 code - 1부터 code칸씩 건너뛰며 골랐다.
This post is licensed under CC BY 4.0 by the author.