We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Remove Comments

Number: 722

Difficulty: Medium

Paid? No

Companies: Uber, Microsoft, Google


Problem Description

Given a list of strings representing lines of C++ source code, remove all the comments. There are two types of comments: line comments (starting with //) and block comments (starting with /* and ending with */). After processing, any line that becomes empty must not be included in the final output.


Key Insights

  • Use a flag to track when you are inside a block comment.
  • For each line, process character by character.
  • Ignore characters inside block comments.
  • When encountering a line comment marker (//) while not in a block comment, ignore the rest of that line.
  • If starting a block comment, mark the flag and skip characters until the block ends.
  • Append the processed line to the result only if it is non-empty and not in a continuing block comment.

Space and Time Complexity

Time Complexity: O(n * m) where n is the number of lines and m is the average length of each line. Space Complexity: O(n * m) in worst case due to storing the output, plus a negligible amount for flags and temporary variables.


Solution

The approach involves iterating through each line and each character in the line while maintaining a flag (in_block) to indicate if the current context is inside a block comment. When not inside a block comment, characters are appended to a temporary list that builds the actual code line. Once a line comment (//) is encountered, skip the rest of the line. When a block comment beginning (/) is found, switch the flag and ignore characters until the block comment is terminated by (/). After processing a line, if not in a block comment and the temporary list has content, join the characters and add the line to the resulting list.


Code Solutions

def remove_comments(source):
    # List to hold the final processed code lines
    result = []
    # Flag to indicate if we are inside a block comment
    in_block = False
    # Temporary list to build the current line's code
    new_line = []
    
    # Iterate over each line in the source code
    for line in source:
        i = 0
        # If not in a block comment start a new temporary list
        if not in_block:
            new_line = []
        while i < len(line):
            # Check for start of block comment when not in block
            if not in_block and i + 1 < len(line) and line[i] == '/' and line[i + 1] == '*':
                in_block = True
                i += 1  # Skip the '*' as well
            # Check for end of block comment when in block
            elif in_block and i + 1 < len(line) and line[i] == '*' and line[i + 1] == '/':
                in_block = False
                i += 1  # Skip the '/' as well
            # Check for line comment when not in a block comment
            elif not in_block and i + 1 < len(line) and line[i] == '/' and line[i + 1] == '/':
                break  # Ignore the rest of the line
            # Append character when not in block comment
            elif not in_block:
                new_line.append(line[i])
            # Move to the next character
            i += 1
        # If current line has valid code and we're not inside a block comment, add it to result
        if new_line and not in_block:
            result.append("".join(new_line))
    return result

# Example usage:
source = ["/*Test program */", "int main()", "{ ", "  // variable declaration ", "int a, b, c;", "/* This is a test", "   multiline  ", "   comment for ", "   testing */", "a = b + c;", "}"]
print(remove_comments(source))
← Back to All Questions