Similar Problems

Similar Problems not available

Remove Comments - Leetcode Solution

LeetCode:  Remove Comments Leetcode Solution

Difficulty: Medium

Topics: string array  

Problem Statement:

Given a C++ program, remove all comments from it.

You need to parse the code and remove all the commented-out code.

A C++ comment begins with //, and extends to the end of the line. For example:

std::cout << "Hello, world!"; // A greeting

A C++ block comment begins with /* and ends with */.

Block comments may extend over multiple lines.

For example:

/* This is a
multiline comment */
std::cout << "Hello, world!";

Solution:

The problem can be solved by iterating through each line of the input program and removing any occurring comments. There are two types of comments: line comments and block comments.

Line comments are easy to remove. If we find two consecutive backslashes "//", then we can consider all characters starting from this position to the end of the line as a comment and ignore this line.

The code snippet for line comment removal would look like this:

while (pos + 1 < len && code[pos] == '/' && code[pos + 1] == '/') {
    while (pos < len && code[pos] != '\n') pos++;
}

Block comments can be a bit more challenging to remove because they can span multiple lines. We need to find the start and end positions of a block comment, and then remove all the characters between the start and end positions.

The code snippet for block comment removal would look like this:

while (pos + 1 < len && code[pos] == '/' && code[pos + 1] == '*') {
    pos += 2;
    while (pos + 1 < len && !(code[pos] == '*' && code[pos + 1] == '/')) {
        pos++;
    }
    pos += 2;
}

To implement the solution, we need to initialize a flag variable in_block_comment to false, which indicates whether we are currently inside a block comment or not.

We iterate through each character of the input program. If the current character is '/' and the next character is '/', we ignore all the characters till we find a new line. If the current character is '/' and the next character is '', we mark the in_block_comment flag to true. If the flag is already true, we just keep ignoring all the characters till we reach '/'. Once we reach '*/', we mark the flag to be false, and continue processing the remaining program.

Code:

Following is the implementation of the remove comments function:

string removeComments(string code) {
    string ans;
    bool in_block_comment = false;
    int n = code.size();
    for (int i = 0; i < n; ) {
        if (in_block_comment) {
            if (code[i] == '*' && i + 1 < n && code[i + 1] == '/') {
                in_block_comment = false;
                i += 2;
            } else {
                i++;
            }
        } else {
            if (code[i] == '/' && i + 1 < n && code[i + 1] == '/') {
                i += 2;
                while (i < n && code[i] != '\n') i++;
            } else if (code[i] == '/' && i + 1 < n && code[i + 1] == '*') {
                in_block_comment = true;
                i += 2;
            } else {
                ans.push_back(code[i++]);
            }
        }
    }
    return ans;
}

Time Complexity: O(n), where n is the length of the input program.

Space Complexity: O(n), where n is the length of the input program.

Remove Comments Solution Code

1