Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

have been devised over the years A common method having to do with computing is

ID: 3746662 • Letter: H

Question

have been devised over the years A common method having to do with computing is the ASCll character set used to display characters on the screen Each character is given In (in duienge, you wa be given ast ng to dec de An example of an encoded stree fotows The tatie below show, the conversion from the string Hacker 0nt to the ASCII strng 729799107101114128297110107 Character ASCI Value 97 101 114 82 97 110 107 For reference, the characters in s correspond to the tollowing ASC values: The value range for A through Z is 65 through 90 The value range for a through z is 97 through 122 The value of the space character is 32 encoded an encoded string

Explanation / Answer

// here is the full code
// output is attached
// please comment if you need any clarification
// hit like if you liked it

// CODE
public class Solution {


    static String decode(String encode) {
        
        // string encode has the encoded string
        // instead of reversing the string, we can parse
        // from right to left
        // first find the length of the string
        int len = encode.length();
        
        StringBuilder decodeStr = new StringBuilder(len); // create a container to hold the decoded string
        
        for(int i = len-1; i >= 0; i--) {
            char d = encode.charAt(i);
            char c;
            switch(d) {
                case '1': // if the digit starting with 1, then
                        // we must read 3 chars to get the number
                        c = (char)(((int)d-48)*100 + ((int)encode.charAt(i-1)-48)*10 + ((int)encode.charAt(i-2)-48));
                    i-=2;
                    break;
                default: // if the digit starting with other number, then
                        // we only need to read 2 chars to get the number
                        c = (char)(((int)d-48)*10 + ((int)encode.charAt(i-1)-48));
                        i-=1;
                    break;
            }
            decodeStr.append(c); // add the char to the decoded string
        }
        return decodeStr.toString(); // return the decoded string to the main
    }

    public static void main(String[] args) {
        String str = "701011792823411101701997927";
        System.out.println(decode(str));
    }
}

// OUTPUT