Skip to content
Problem 722 · LeetCode 722(opens in a new tab)

Remove Comments

MediumArrays

You are given a C++ program as a list of lines, source. Strip out its comments and return the lines that are left. There are two kinds: // hides itself and everything after it on that line, and /* hides everything up to the next */, even across several lines. The */ must come after the /*, so /*/ does not open and close a comment.

Whichever comment starts first wins: a // inside a block comment means nothing, and neither does a /* inside a line comment. When a block comment spans lines, the line breaks inside it vanish too, so the code before it and the code after it join into one line. Leave out every line that ends up empty.

Example 1

Input:  source = ["x = 1; // set x", "/* note",
                  "   more */ y = 2;", "return x;"]
Output: ["x = 1; ", " y = 2;", "return x;"]
Why:    the block comment covers "/* note" and "   more */"
2 more examples and the constraints

Example 2

Input:  source = ["a/*b", "c*/d", "e//f"]
Output: ["ad", "e"]
Why:    the block swallows a line break, so a and d share a line

Example 3

Input:  source = ["/*/ x */y"]
Output: ["y"]
Why:    the slash right after /* does not close it; */ does

Constraints

  • 1 ≤ number of lines ≤ 100
  • 0 ≤ length of each line ≤ 80
  • Lines hold printable ASCII with no quote characters
  • Every block comment is closed

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Join, scan, splitO(n)
  2. Line by line with a carried bufferO(n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 100 lines of 80 charactersVerdict
Join, scan, splitO(n)
Simple; copies the text twice
Line by line with a carried bufferO(n)
Best; reads lines as given

n = total characters in the program.

Edge cases to test

  • A block spanning lines. ["a/*b", "c*/d"] gives ["ad"]: the two halves join.
  • `/*/` is not a whole comment. ["/*/ x */y"] gives ["y"].
  • Comment markers inside comments. // inside a block and /* after // mean nothing.
  • Lines that become empty. A line that was all comment, or was empty to begin with, is dropped.
  • Spaces only. " // note" leaves " ", which is not empty, so it stays.

Common mistakes

  • Saving the buffer at the end of every line, even inside a block, which splits code a block comment joined.
  • Moving past only one character of /*, so /*/ closes itself.
  • Dropping lines that hold only spaces; only truly empty lines go.

How to talk through it

  1. 1Clarify. Can comments span lines? Do quotes matter? Should empty lines be dropped?
  2. 2Name the state. One flag, in a block or not, decides how each character is read.
  3. 3Handle the line breaks. Inside a block a line break vanishes, so keep the buffer until the block closes.
  4. 4Code the scan. Check the block flag first, then /*, then //, then keep the character.
  5. 5Test out loud. Run /*/, a block over two lines and a line that is all comment.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?