algoadvance

LeetCode Problem 722: Remove Comments

You are given a list of source code source where source[i] is the i-th line of the source code. This is formatted in a list where each string is a single line of the code.

Comments can appear in three forms:

  1. Block comments start with /* and end with */. They can span multiple lines.
  2. Line comments start with // and end at the end of the same line.
  3. A string can contain either comment type.

The task is to return the source code after removing the comments and keeping the format of the original code lines which are not part of any comment.

Clarifying Questions

  1. Can the input source code be empty?
    • Yes, the input could be an empty list.
  2. Are there any guarantees about the input?
    • The lines will only contain valid code and comment syntax.
  3. Should whitespace be preserved?
    • Yes, ensure the whitespace is preserved except for the comments.
  4. What constitutes valid code for consideration?
    • Any part of the source that is not within a comment block or line.

Strategy

  1. Initialization:
    • Create a list to hold the output lines of code.
    • Create a boolean flag to track when within a block comment.
  2. Processing Each Line:
    • Traverse each line of the source code:
      • If in a block comment, search for the end of the block (*/).
      • If not in a block comment, search for the start of a line comment (//) or block comment (/*).
      • Handle the case where both line and block comments are in the same line.
      • Only add to the output lines if there is non-comment code outside of block comments.
  3. Building the Output:
    • Append lines without comments into the result list.
    • Skip lines or parts of lines enclosed in comments.

Time Complexity

Code

Here’s the implementation in Python:

def removeComments(source):
    in_block_comment = False
    result = []
    new_line = []
    
    for line in source:
        i = 0
        while i < len(line):
            if in_block_comment:
                if line[i:i + 2] == '*/':
                    in_block_comment = False
                    i += 2
                else:
                    i += 1
            else:
                if line[i:i + 2] == '/*':
                    in_block_comment = True
                    i += 2
                elif line[i:i + 2] == '//':
                    break
                else:
                    new_line.append(line[i])
                    i += 1
        if not in_block_comment and new_line:
            result.append("".join(new_line))
            new_line = []
    
    return result

# Example usage:
source_code = [
    "/*Test program */", 
    "int main()", 
    "{ ", 
    "  // variable declaration ", 
    "int a, b, c;", 
    "/* This is a test", 
    "   multiline  ", 
    "   comment for ", 
    "   testing */", 
    "a = b + c;", 
    "}"
]
print(removeComments(source_code))

This implementation addresses the problem by carefully checking for each type of comment indicator and managing the state transitions between inside and outside of comments.

Try our interview co-pilot at AlgoAdvance.com