Given a C++ program, remove comments from it. The program source is an array of strings source
where source[i]
is the ith
line of the source code. This represents the result of splitting the original source code string by the newline character '\n'
.
In C++, there are two types of comments, line comments, and block comments.
"//"
denotes a line comment, which represents that it and the rest of the characters to the right of it in the same line should be ignored."/*"
denotes a block comment, which represents that all characters until the next (non-overlapping) occurrence of "*/"
should be ignored. (Here, occurrences happen in reading order: line by line from left to right.) To be clear, the string "/*/"
does not yet end the block comment, as the ending would be overlapping the beginning.The first effective comment takes precedence over others.
"//"
occurs in a block comment, it is ignored."/*"
occurs in a line or block comment, it is also ignored.If a certain line of code is empty after removing comments, you must not output that line: each string in the answer list will be non-empty.
There will be no control characters, single quote, or double quote characters.
source = "string s = "/* Not a comment. */";"
will not be a test case.Also, nothing else such as defines or macros will interfere with the comments.
It is guaranteed that every open block comment will eventually be closed, so "/*"
outside of a line or block comment always starts a new comment.
Finally, implicit newline characters can be deleted by block comments. Please see the examples below for details.
After removing the comments from the source code, return the source code in the same format.
Example 1:
Input: source = ["/*Test program */", "int main()", "{ ", " // variable declaration ", "int a, b, c;", "/* This is a test", " multiline ", " comment for ", " testing */", "a = b + c;", "}"] Output: ["int main()","{ "," ","int a, b, c;","a = b + c;","}"] Explanation: The line by line code is visualized as below: /*Test program */ int main() { // variable declaration int a, b, c; /* This is a test multiline comment for testing */ a = b + c; } The string /* denotes a block comment, including line 1 and lines 6-9. The string // denotes line 4 as comments. The line by line output code is visualized as below: int main() { int a, b, c; a = b + c; }
Example 2:
Input: source = ["a/*comment", "line", "more_comment*/b"] Output: ["ab"] Explanation: The original source string is "a/*comment\nline\nmore_comment*/b", where we have bolded the newline characters. After deletion, the implicit newline characters are deleted, leaving the string "ab", which when delimited by newline characters becomes ["ab"].
Constraints:
1 <= source.length <= 100
0 <= source[i].length <= 80
source[i]
consists of printable ASCII characters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to removing comments involves meticulously examining the code line by line and character by character. We systematically check for the start and end markers of both block and line comments. This exhaustive search guarantees that all comments, according to the language's rules, are identified and removed.
Here's how the algorithm would work step-by-step:
def remove_comments_brute_force(program_code):
in_block_comment = False
result = []
index = 0
while index < len(program_code):
# Check for start of block comment
if program_code[index:index + 2] == '/*':
in_block_comment = True
index += 2
# If inside a block comment, look for the end
elif in_block_comment:
if program_code[index:index + 2] == '*/':
# Found end of block comment, resume normal parsing
in_block_comment = False
index += 2
else:
index += 1
# Check for start of line comment
elif program_code[index:index + 2] == '//':
# Skip the rest of the line
index = program_code.find('
', index)
if index == -1:
break
index += 1
else:
# Append character if not in comment
result.append(program_code[index])
index += 1
return "".join(result)
The goal is to process text and strip out comments. We need to carefully go through the text character by character, keeping track of whether we're inside a comment or not, and only keeping characters outside of comments.
Here's how the algorithm would work step-by-step:
def remove_comments(program_text):
result = ''
in_single_line_comment = False
in_multi_line_comment = False
index = 0
while index < len(program_text):
if in_single_line_comment:
if program_text[index] == '
':
in_single_line_comment = False
result += '
'
index += 1
elif in_multi_line_comment:
if program_text[index:index + 2] == '*/':
in_multi_line_comment = False
index += 2
else:
index += 1
else:
if program_text[index:index + 2] == '//':
# Begin single-line comment
in_single_line_comment = True
index += 2
elif program_text[index:index + 2] == '/*':
# Begin multi-line comment
in_multi_line_comment = True
index += 2
else:
# Append current character to result.
result += program_text[index]
index += 1
return result
Case | How to Handle |
---|---|
Null or empty input source array | Return an empty list immediately to handle the invalid input gracefully. |
Empty source code line (empty string) | Treat it as a line with no comments and add it directly to the result if no multiline comment is active. |
Single line comment at the very beginning of the line | Entire line should be ignored after encountering the //. |
Multiline comment at the very beginning of a line | Start ignoring content until the closing tag '*/' is found. |
Multiline comment that spans multiple lines. | Maintain a flag to indicate if inside a multiline comment and process each line accordingly. |
Single line comment inside a multiline comment. | It is ignored, multiline comment takes precedence until the closing '*/' is found. |
Multiline comment followed immediately by a single-line comment on the same line. | After closing the multiline comment, process the remaining part of the line considering the single-line comment if present. |
Overlapping comments like '/* ... // ... */' | Multiline comments have precedence, so '//' should be ignored inside a multiline comment. |