CodeWars 예순 여섯 번째 문제
Updated:
Simple Encryption #1 - Alternating Split
public static String encrypt(final String text, final int n) {
String encryptText = text;
for(int i = 0; i < n ; i++) {
String tempText = "";
for(int j = 1; j < text.length(); j+=2) {
tempText += encryptText.substring(j, j+1);
}
for(int j = 0; j < text.length(); j+=2) {
tempText += encryptText.substring(j, j+1);
}
encryptText = tempText;
}
return encryptText;
}
public static String decrypt(final String encryptedText, final int n) {
String decryptText = encryptedText;
for(int i = 0; i < n ; i++) {
String tempText = "";
String even = decryptText.substring(0, decryptText.length()/2);
String odd = decryptText.substring(decryptText.length()/2, decryptText.length());
for(int j = 0; j < even.length(); j++) {
tempText += odd.substring(j,j+1);
tempText += even.substring(j,j+1);
}
if(odd.length() !=even.length()) {
tempText += odd.substring(odd.length()-1);
}
decryptText = tempText;
}
return decryptText;
}
- 문제 해석이 어려웠다. 처음에는 무슨 소리인가 했는데 알고 보니 짝수번째 문자들을 맨앞으로 보내면 되는 것이었다.
- 영어공부가 이래서 중요한가 보다.
- 어쨌든, 알고리즘은 단순했다.
- Best 알고리즘의 경우 재귀함수로 풀었던데 대단하구만. 최근 재귀함수를 공부했는데 아직도 완벽하게 이해를 못하는 것 같다. 아니 반복문이 익숙해서 그런가