Codeforces 32B Borze Solution & Explanation

Difficulty : 800

Problem Description

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.

Input

The first line contains a number in Borze code. The length of the string is between 1 and 200 characters. It’s guaranteed that the given string is a valid Borze code of some ternary number (this number can have leading zeroes).

Output

Output the decoded ternary number. It can have leading zeroes.

Examples

Input.-.–
Output012
Input–.
Output20
Input-..-.–
Output1012

Solution

We first observe the problem statement: ‘It’s guaranteed that the given string is a valid Borze code’, which means we don’t need to worry about handling invalid inputs. We can use a simple for loop to determine how to convert the Borze code.

C# Solution

Solution1

using System.Text;

string str = Console.ReadLine();
char[]charArr = str.ToCharArray();
StringBuilder res = new StringBuilder();

for(int i=0; i<charArr.Length; i++){
    
    if(charArr[i]=='.'){
        res.Append("0");
    }
    else if(charArr[i+1]=='.'){
        res.Append("1");
        i+=1;
    }
    else{
        res.Append("2");
        i+=1;
    }
}

Console.WriteLine(res.ToString());

We also utilize StringBuilder to prevent performance degradation caused by repetitive string concatenation.

Klook.com

Conclusion

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 32B – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *