2017. 09. 24 - TIL

09.24

오늘 한 일

  • 알고리즘 문제 풀이 ``` package nhnEnter2017;

public class Question02 {

public String solution(String input) {
	String[] splitinput = input.split(" ");
	String order = splitinput[0];
	char[] scretKey = splitinput[1].toCharArray();
	int lotateNum = Integer.parseInt(splitinput[2]);
	char[] message = splitinput[3].toCharArray();
	String messageStr;
	String result;
	lotateNum = lotateNum % message.length;

	if(order.equals("encrypt")) {
		for(int i=0; i<message.length; i++) {
			message[i] += scretKey[i] - 97;
			if (message[i] > 122) {
				message[i] -= 26;
			}
		}

		if (lotateNum > 0) {
			messageStr = new String(message, 0, message.length);
			result = messageStr.substring(lotateNum) + messageStr.substring(0, lotateNum);

		} else if(lotateNum < 0) {
			messageStr = new String(message, 0, message.length);
			result = messageStr.substring(messageStr.length() - lotateNum) + messageStr.substring(0, messageStr.length() - lotateNum);
		} else {
			result = new String(message, 0, message.length);
		}

		return result;			
	} else {
		if (-lotateNum > 0) {
			messageStr = new String(message, 0, message.length);
			result = messageStr.substring(lotateNum) + messageStr.substring(0, lotateNum);

		} else if(-lotateNum < 0) {
			messageStr = new String(message, 0, message.length);
			result = messageStr.substring(messageStr.length() - lotateNum) + messageStr.substring(0, messageStr.length() - lotateNum);
		} else {
			result = new String(message, 0, message.length);
		}

		message = result.toCharArray();

		for(int i=0; i<message.length; i++) {
			message[i] -= scretKey[i] - 97;
			if (message[i] < 97) {
				message[i] += 26;
			}
		}

		return new String(message, 0, message.length);
	}
}

public static void main(String[] args) {
	Question02 q2 = new Question02();
	System.out.println(q2.solution("encrypt secretword 3 helloworld"));
	System.out.println(q2.solution("decrypt secretword 3 cspkfcgzin"));		
} }

```

Comments