• + 0 comments

    More of my solutions with step by step guidance - leetcode.

    import re
    
    
    def decode(code: str) -> str:
        """If there are symbols or spaces between two alphanumeric characters
        of the decoded script, then the function replaces them with a single space ''
        for better readability.
        The function doesn't use 'if' conditions for decoding.
        Alphanumeric characters consist of: [A-Z, a-z, and 0-9].
        >>> decode("This$#is% Matrix#  %!")
        This is Matrix#  %!
        >>> decode("This%%isMatrix#scrpt&%!&")
        This isMatrix scrpt&%!&
        """
        # Define a regular expression pattern to match non-alphanumeric characters
        # between alphanumeric characters
        pattern = r"(?<=[a-zA-Z0-9])[^a-zA-Z0-9]+(?=[a-zA-Z0-9])"
    
        # Use re.sub() to replace matched patterns with a single space
        decoded = re.sub(pattern, " ", code)
    
        # Return the decoded string
        return decoded
    
    
    # Read the number of rows and columns from input
    rows, columns = map(int, input().split())
    
    # Read the matrix of characters row by row
    matrix_dec, str_decoded = [input() for _ in range(rows)], ""
    
    # Iterate through columns and rows to create a string by combining characters from the matrix
    for col in range(columns):
        for row in range(rows):
            str_decoded += matrix_dec[row][col]
    
    # Print the result of decoding the created string
    print(decode(str_decoded))