Tap here to switch tabs
Problem
Submissions

Problem: Remove Comments

med
30 min
Try to solve the Remove Comments problem.

Statement

Given an array of strings source representing a program source code where each element is a single line, return the source code after removing all comments.

There are two kinds of comments in the code:

  • Line comment: Begins with // and indicates that all characters from // to the end of the current line should be ignored.

  • Block comment: Begins with /* and ends with the first subsequent occurrence of */. All characters between /* and */, including newline characters spanning multiple lines, should be ignored.

The comments are processed from left to right. When searching for comment delimiters, the first delimiter encountered takes precedence:

  • If a line comment starts inside a block comment, it is ignored.

  • If a block comment starts inside a line comment, it is ignored.

  • If a block comment spans across multiple lines, the remaining non comment parts of the first and last lines are joined together into a single line.

  • Any line that becomes empty after removing comments should be omitted from the output.

Constraints:

  • 1≤1 \leq source.length ≤100\leq 100

  • 0≤0 \leq source[i].length ≤80\leq 80

  • source[i] consists of printable ASCII characters.

  • Every open block comment is eventually closed.

  • There are no single quote or double quote characters in the input.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Remove Comments

med
30 min
Try to solve the Remove Comments problem.

Statement

Given an array of strings source representing a program source code where each element is a single line, return the source code after removing all comments.

There are two kinds of comments in the code:

  • Line comment: Begins with // and indicates that all characters from // to the end of the current line should be ignored.

  • Block comment: Begins with /* and ends with the first subsequent occurrence of */. All characters between /* and */, including newline characters spanning multiple lines, should be ignored.

The comments are processed from left to right. When searching for comment delimiters, the first delimiter encountered takes precedence:

  • If a line comment starts inside a block comment, it is ignored.

  • If a block comment starts inside a line comment, it is ignored.

  • If a block comment spans across multiple lines, the remaining non comment parts of the first and last lines are joined together into a single line.

  • Any line that becomes empty after removing comments should be omitted from the output.

Constraints:

  • 1≤1 \leq source.length ≤100\leq 100

  • 0≤0 \leq source[i].length ≤80\leq 80

  • source[i] consists of printable ASCII characters.

  • Every open block comment is eventually closed.

  • There are no single quote or double quote characters in the input.