Borze 32-B Codeforces solution in Java

Milind Dinesh
2 min readMay 9, 2024

--

This is a simple solution for the problem B. Borze from Codeforces implemented using Java.

Problem statement

Ternary numeric notation is quite popular in Berland. To telegraph the ternary number the Borze alphabet is used. Digit 0 is transmitted as «.», 1 as «-.» and 2 as « — ». You are to decode the Borze code, i.e. to find out the ternary number given its representation in Borze alphabet.

Sample cases

Sample input and output

Logic

According to the explanation given , all we have to do is convert the Borze alphabets to digits. Each digit is represented by either “ .” , or a combination of “ . ” and a “ — ”. We could just iterate through the characters of the input string and then decode it character by character. If the character is a “ . ” , we convert it to 0 . If it’s a “ — ” , we also check the next character to find out whether it needs to be decoded to 1 or 2 .

Code

import java.util.Scanner;
public class Borze{
public static void main(String[] args){
String input;
Scanner scanner = new Scanner(System.in);
input = scanner.nextLine();

String output = "";
for (int i = 0;i<input.length();i++){
if (input.charAt(i) == '.'){
output += "0";
}
else if (input.charAt(i)=='-' && input.charAt(i+1)=='.') {
output += "1";
++i;
}
else {
output += "2";
++i;
}
}
System.out.println(output);
scanner.close();
}
}

The lines ++i , increments the value of i by 1 before getting incremented again by the for loop . This is to move the index to the next character since we have already checked the characters at the indices i and i +1 .

Now I do feel like the code would be way shorter if I did it in python , but still , I wanted to try this out in Java for a change. Feel free to let me know what you think! I’m also open to suggestions for other Codeforces problem to try out.

--

--

Milind Dinesh

I like learning about cybersecurity and also to code.