Remove Comments - Problem

Given a C++ program, remove comments from it. The program source is an array of strings source where source[i] is the i-th line of the source code.

In C++, there are two types of comments:

  • Line comments: The string // 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.
  • Block comments: The string /* denotes a block comment, which represents that all characters until the next occurrence of */ should be ignored.

Important rules:

  • The first effective comment takes precedence over others
  • If // occurs in a block comment, it is ignored
  • If /* occurs in a line or block comment, it is also ignored
  • If a line becomes empty after removing comments, do not output that line
  • Block comments can span multiple lines and delete implicit newline characters

Return the source code with comments removed in the same format.

Input & Output

Example 1 — Mixed Comments
$ Input: source = ["/*Test program */", "int main()", "{ ", " // variable declaration ", "int a, b, c;", "/* This is a long ", " comment to test ", " if the comment is", " removed properly.*/", "a = b + c;", "}"]
Output: ["int main()","{ ","int a, b, c;","a = b + c;","}"]
💡 Note: Block comment /*Test program */ removed from first line making it empty. Line comment // variable declaration removes rest of line. Multi-line block comment removed completely. Only actual code lines remain.
Example 2 — Block Comment Across Lines
$ Input: source = ["a/*comment", "line", "more_comment*/b"]
Output: ["ab"]
💡 Note: Block comment spans multiple lines from 'a/*comment' through 'more_comment*/b'. After removing the comment content, 'a' and 'b' are concatenated since the block comment removal eliminates the newline characters in between.
Example 3 — Line Comment Only
$ Input: source = ["class Test {", " public static void main() {", " // This line is comment", " System.out.println(\"Hello\");", " }", "}"]
Output: ["class Test {"," public static void main() {"," System.out.println(\"Hello\");"," ","}"]
💡 Note: Line comment '// This line is comment' removes the entire line. All other lines containing actual code are preserved exactly as they are.

Constraints

  • 1 ≤ source.length ≤ 100
  • 0 ≤ source[i].length ≤ 80
  • source[i] consists of printable ASCII characters
  • Every open block comment will eventually be closed
  • There are no string literals with quotes

Visualization

Tap to expand
Remove Comments: Input → Output TransformationINPUT SOURCE/*Test program */int main()// variable declarationint a, b, c;/* comment */FILTERCLEAN OUTPUTint main()int a, b, c;(empty lines removed)// Line Comments/* Block Comments */Code OnlyComments Removed → Only Working Code RemainsBlock comments can span multiple linesLine comments remove everything after //
Understanding the Visualization
1
Input Code
C++ source with line and block comments
2
Comment Filtering
Remove // and /* */ comment regions
3
Clean Output
Array of non-empty code lines only
Key Takeaway
🎯 Key Insight: Use a state machine to track block comment regions while processing characters sequentially
Asked in
Google 15 Microsoft 12 Amazon 8 Facebook 6
32.0K Views
Medium Frequency
~25 min Avg. Time
487 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen