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 from C++ Code INPUT: Source Code ["/*Test program */", "int main()", "{ ", " // variable decl ", "int a, b, c;", "/* This is a long ", " comment to test ", " if the comment is", " removed properly.*/", "a = b + c;", "}"] Line comment (//) Block comment (/* */) Keep (valid code) ALGORITHM: Single Pass 1 Initialize State inBlock = false newLine = "" (builder) 2 Scan Each Character Check for /* or // Check for */ if in block 3 Handle Comments // --> skip rest of line /* --> set inBlock=true */ --> set inBlock=false 4 Build Result Add char if not in comment At line end: if newLine not empty, add to result Normal In Block /* */ RESULT: Clean Code ["int main()", "{ ", "int a, b, c;", "a = b + c;", "}"] OK - Comments Removed Lines removed: 6 Lines kept: 5 Block comments: 2 Line comments: 1 Key Insight: Track state with a boolean flag (inBlock) to know if we're inside a block comment. Process character by character, looking ahead for // and /* patterns. Build each line incrementally, only adding to result if non-empty. Block comments can span multiple lines! TutorialsPoint - Remove Comments | Single Pass with String Building Approach
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