본문 바로가기
Language/Algorithm

Cryptogram

by Zayne 2023. 12. 2.

Example

Input : 4 #****###**#####**#####**##**
Output : COOL

 

My answer : 

import java.util.Scanner;

class BinaryConverter {
	public static void main(String[] args) {
		BinaryConverter t = new BinaryConverter();
		Scanner kb = new Scanner(System.in);
		int a = kb.nextInt();
		String s = kb.next();
		System.out.println(t.solution(a,s));
	}

	String solution(int a, String s) {
		String answer = "";
		String stringToDigit = s.replace('#','1').replace('*','0');

		for(int i = 0; i < a; i++) {
			String tmp = stringToDigit.substring(7*i, 7+7*i);
			answer += (char) Integer.parseInt(tmp,2);
		}

		return answer;
	}
}

 

  1. In the 'solution' method, the input string 's' is modified by replacing '#' with '1' and '*' with '0'.
  2. The modified binary string is then processed in chunks of 7 characters, and each chunk is converted to its decimal equivalent using 'Integer.parseInt(tmp, 2)'.
  3. The decimal value is cast to a character and appended to the 'answer' string.
  4. The final 'answer' string, which represents the ASCII characters corresponding to the binary input, is returned.

'Language > Algorithm' 카테고리의 다른 글

Increasing Sequence Counter  (0) 2023.12.05
The elements that are greater than their predecessors  (2) 2023.12.04
String Compression  (4) 2023.12.02
Calculate Minimum Distances  (1) 2023.09.02
Extract Digits from String  (0) 2023.09.01