Decode all possibilities from a given encoded ascii string


  • 0
    P

    // We encode a string, s, by performing the following sequence of actions:
    // Replace each character with its ASCII value representation.
    // Reverse the string.
    //
    // For example, the table below shows the conversion from the string "Go VMWare" to the ASCII string "711113286778797114101":
    //
    // Character G o V M W a r e
    // ASCII Value 71 111 32 86 77 87 97 114 101
    //
    // We then reverse the ASCII string to get the encoded string 101411797877682311117.
    // For reference, the characters in s are ASCII characters within the range 10 - 126 which include special characters.
    // Complete the decode function in the editor below. It has one parameter:
    // encoded - A reversed ASCII string denoting an encoded string s.
    // The function must decode the encoded string and return the list of ways in which s can be decoded.

    ,,,
    static Collection<String> decode(String encoded) {

    return Collection<String>
    }
    ,,,


  • 0
    M
    (function(){
        "use strict";
        function decodeString(str){
            str = str.split("").reverse().join("").split(32);
            let sentance = []
            for(let i=0; i<str.length; i++){
                let asciiWord = str[i];
                sentance.push(_getword(asciiWord));
            }
            console.log(sentance.join(" "));
        };
    
        function _getword(asciiWord){
            let a = "";
            let results = [];
            for(let i=0; i<asciiWord.length; i++){
                a += asciiWord[i];
                if(Number(a)>=65 && Number(a)<=99){
                    results.push(String.fromCharCode(a));
                    a = "";
                }else if(Number(a)>=100 && Number(a)<=122){
                    results.push(String.fromCharCode(a));
                    a = "";
                }
            }
            return results.join("");
        } 
        
        decodeString("64101301011794019923111611236112117900179231116112312161150180150189792310140161123511501231019901110130150180180110161101137");
    
    }())
    

  • 0
    P
    solutions = []
    currentSolution = ''
    unprocessed = ''
    
    
    def decode(s):
        flipped = s[::-1]
        global solutions
        global unprocessed
        global currentSolution
        currentSolution = ''
        unprocessed = flipped
        _decode()
        return solutions
    
    
    def is_valid(split):
        if split.startswith('0'):
            return False
        value = int(split)
        if value < 10 or value > 126:
            return False
        return True
    
    
    def _decode():
        global unprocessed
        global currentSolution
        global solutions
        if len(unprocessed) == 0:
            solutions.append(currentSolution)
        else:
            possible_splits = list()
            possible_splits.append(unprocessed[0:2])
            possible_splits.append(unprocessed[0:3])
    
            for split in possible_splits:
                if is_valid(split):
                    decoded_character = chr(int(split))
                    currentSolution += decoded_character
                    unprocessed = unprocessed[len(split):]
                    _decode()
                    currentSolution = currentSolution[0: len(currentSolution) - 1]
                    unprocessed = split + unprocessed
    
    
    def main():
        final_solutions = decode('0018014111117811180180110127')
        print(final_solutions)
    
    
    if __name__ == '__main__':
        main()
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.