Last Updated: 24 Feb, 2021

Moderate

## Problem statement

#### Your task is to remove the comments from the given text and print the text in the same format.

##### Note:
``````The first comment is given precedence over the other i.e. if the string “//” occurs in a block comment, then it is ignored and/or if the string “/*” occurs in a line or a block comment, it is also ignored.

The starting and ending point of the block comment must be non-overlapping i.e. a string like “/*/” does not end the block comment, as the ending would be overlapping the beginning.

Every starting point of a block comment will have an ending point as well.

The (implicit) newline characters which may occur when a block comment spans multiple lines, must be deleted/removed.

The text doesn’t contain any single or double quotes characters.
``````
##### Input format:
``````The very first line of input contains an integer ‘N’ denoting the number of lines in the given text.

Now ‘N’ lines follow where the ith line contains a string that denotes the ith line of the text.
``````
##### Output format:
``````For each test case, print the text, in the same format, obtained after removing the comments.
``````
##### Note:
``````If a certain line of the text becomes empty after removing the comments, you must not output that line i.e. each string in the resulting text must be non-empty.

You do not need to print anything, it has already been taken care of. Just return the resulting text in the same format.
``````
##### Constraints:
``````1 <= N <= 100
0 <= Length of ith String <= 5 * 10^3

Where  ‘N’ represents the number of lines in the given text.

Time Limit: 1 sec
``````

## Approaches

### 01 Approach

• The idea behind this approach is to parse the given text by iterating over the characters line by line.
• While parsing the text, we also maintain a state (or a variable). The state helps us to determine whether the current character is inside a block comment or not.
• Now, whenever we encounter the starting point of a block comment (i.e. “/*”), we set the state to ‘1’ (or true) and keep ignoring the following characters (as they are part of the comment) until we encounter the endpoint of the block comment.
• On encountering the endpoint of the block comment, we set the state to ‘0’ (or false).
• If instead of a block comment we encounter the starting point of a line comment (i.e. “//”) then we ignore the remaining characters of that line and start parsing the next line.
• In case the current character is not part of a comment, then we add the character to the corresponding line in the resulting text.
• In this way, we parse the complete text and remove the comments.

### Algorithm:

• Create an array ans, to store the resulting text (after removing the comments) in the form of an array of strings.
• Create another variable say, insideBlock, which will be used to maintain the current state while parsing the text.
• Create an empty string, say parsedStr, to store the parsed form of the current line of the text.
• Repeat the following steps for each line of the text:
• For each character in the current line:
• If insideBlock = true, then the current character is part of a block comment. Now, we check if we have encountered the endpoint of the block comment.
• If the string formed by the current character and the next character is “*/”, then we have encountered the endpoint of the block comment. So, set the state to ‘false’ i.e. insideBlock = false, and skip the next character.
• Otherwise, move on to the next character.
• Otherwise, the current character is not part of a block comment. So, we check if we have encountered a starting point for any type of comment.
• If the string formed by the current character and the next character is “/*”, then we have encountered the starting point of a block comment. So, set the state to ‘true’ i.e. insideBlock = true, and skip the next character.
• Otherwise, if the string formed by the current character and the next character is “//”, then we have encountered the starting point of a line comment. So, skip all the following characters by breaking out of the loop.
• Otherwise, the current character is not part of any comment. So, add the character to the parsed string, parsedStr, and move on to the next character.
• If the state is set to false, i.e. insideBlock = false, then the block comment doesn’t span to the next line. So, we can add the parsed string to the resulting text:
• If the parsed string is not empty, then add it to the ans array.
• Clear the parsed string, i.e. set parsedStr = “”.
• Return the ans array.