Skip to content

Bug Report for string-encode-and-decode #4378

@abby-diamond

Description

@abby-diamond

Bug Report for https://neetcode.io/problems/string-encode-and-decode

Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.

Bug description: using $ as a delimiter in the Python version of the problem causes the test script to throw an error.

Working example using & as delimiter:

class Solution:

    def encode(self, strs: List[str]) -> str:
        output = []
        for string in strs:
            length = len(string)
            output.append(''.join([str(length), '&', string]))
        return ''.join(output)
        

    def decode(self, s: str) -> List[str]:
        output = []
        i = 0
        while i < len(s):
            length = []

            while s[i] != '&':
                length.append(s[i])
                i += 1
            length = int(''.join(length))
            
            # skip over delimiter
            i += 1

            output.append(s[i:i+length])
            i += length
        return output

Same example, except using $ as delimiter

class Solution:

    def encode(self, strs: List[str]) -> str:
        output = []
        for string in strs:
            length = len(string)
            output.append(''.join([str(length), '$', string]))
        return ''.join(output)
        

    def decode(self, s: str) -> List[str]:
        output = []
        i = 0
        while i < len(s):
            length = []

            while s[i] != '$':
                length.append(s[i])
                i += 1
            length = int(''.join(length))
            
            # skip over delimiter
            i += 1

            output.append(s[i:i+length])
            i += length
        return output

Error thrown:

File "/box/script.py", line 7
    output.append(''.join([str(length), '
                                        ^
SyntaxError: unterminated string literal (detected at line 7)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions