Leetcode 722: Remove Comments
You are given a C++ program represented as an array of strings, where each string corresponds to a line of code. Your task is to remove all comments from the code. The two types of comments in C++ are line comments (//) and block comments (/* … */). After removing comments, return the modified code as an array of strings, excluding any empty lines.
Problem
Approach
Steps
Complexity
Input: An array of strings representing lines of a C++ program.
Example: ["/*This is a test program*/", "int main()", "{", " // This is a variable declaration", "int a, b, c;"]
Constraints:
• 1 <= source.length <= 100
• 0 <= source[i].length <= 80
• No single or double quotes in the input
• Every open block comment will eventually be closed
Output: Return the modified program as an array of strings where all comments are removed, and empty lines are excluded.
Example: ["int main()", "{", " int a, b, c;", " a = b + c;", "}"]
Constraints:
• Each output string will be non-empty.
Goal: To remove comments from the given C++ program code and return the cleaned code.
Steps:
• Iterate through each line of the source code.
• Identify and skip characters inside block comments (/*...*/).
• Identify line comments (//) and ignore the remainder of the line.
• Ensure that lines which become empty after removing comments are not included in the result.
Goal: The input will always contain valid and properly closed comments.
Steps:
• Each string in the input contains printable ASCII characters.
• There will be no single-quote or double-quote characters.
• Every open block comment will eventually be closed.
Assumptions:
• The input is always valid with properly closed comments.
• There are no macros or defines that affect the comments.
• Input: Example 1
• Explanation: In this example, the block comment removes multiple lines and part of a line, while the line comment removes everything after the '//' in the line.
• Input: Example 2
• Explanation: Here, the block comment starts and ends in the middle of the string, removing implicit newlines between 'a' and 'b'.
Approach: The solution uses an iterative approach to process each line of code, removing comments based on whether the code is inside a block comment or a line comment.
Observations:
• The code must handle two types of comments: line comments and block comments.
• Block comments can span multiple lines, and a line comment is limited to the current line.
• A simple loop that processes each character in the string will help identify and remove comments.
Steps:
• Initialize a variable to track if we are inside a block comment.
• Iterate through each line and each character in the line.
• Skip characters inside block comments, and ignore the remainder of the line when a line comment is detected.
• When a block comment ends, resume normal processing of the line.
Empty Inputs:
• Handle empty input arrays gracefully.
Large Inputs:
• The solution should efficiently handle inputs of maximum length.
Special Values:
• Ensure that multiline block comments and line comments are processed correctly even when nested.
Constraints:
• Ensure that every block comment is closed, as guaranteed by the problem constraints.
vector<string> removeComments(vector<string>& source) {
vector<string> ans;
bool is_mul = false;
string res = "";
for(int i = 0; i < source.size(); i++) {
string str = source[i];
res = is_mul? res:"";
for(int j = 0; j < str.size() ; j++) {
if(!is_mul) {
if (j < str.size() - 1 && str[j] == '/' && str[j + 1] == '/') {
break;
} else if (j < str.size() - 1 && str[j] == '/' && str[j + 1] == '*') {
is_mul = true;
j++;
} else {
res += str[j];
}
} else {
if(j < str.size() - 1 && str[j] == '*' && str[j + 1] == '/') {
is_mul = false;
j++;
}
}
}
if(!is_mul && res != "") ans.push_back(res);
}
return ans;
}
};
//
/*
*/
/* */
1 : Function Definition
vector<string> removeComments(vector<string>& source) {
Defines the 'removeComments' function that takes a reference to a vector of strings and returns a vector of strings after removing comments.
2 : Variable Initialization
vector<string> ans;
Declares a vector 'ans' that will store the final result after comments are removed.
3 : Variable Initialization
bool is_mul = false;
Declares a boolean variable 'is_mul' to track whether multi-line comments are being processed.
4 : Variable Initialization
string res = "";
Declares a string variable 'res' to accumulate non-comment characters.
5 : Looping Through Source
for(int i = 0; i < source.size(); i++) {
Starts a loop to iterate over each string in the 'source' vector.
6 : Processing Current String
string str = source[i];
Assigns the current string in the vector to 'str' for further processing.
7 : Resetting Result
res = is_mul? res:"";
If currently inside a multi-line comment, keeps 'res' unchanged, otherwise resets it to an empty string.
8 : Inner Loop
for(int j = 0; j < str.size() ; j++) {
Starts an inner loop to process each character of the current string 'str'.
9 : If Not In Multi-line Comment
if(!is_mul) {
Checks if the current character is not inside a multi-line comment.
10 : Handling Single-line Comment
if (j < str.size() - 1 && str[j] == '/' && str[j + 1] == '/') {
Checks for the start of a single-line comment ('//').
11 : Breaking for Single-line Comment
break;
If a single-line comment is detected, breaks out of the loop to stop processing further characters in the line.
12 : Handling Multi-line Comment Start
} else if (j < str.size() - 1 && str[j] == '/' && str[j + 1] == '*') {
Checks for the start of a multi-line comment ('/*').
13 : Entering Multi-line Comment
is_mul = true;
Marks the beginning of a multi-line comment.
14 : Skipping Character in Multi-line Comment
j++;
Skips the next character in the string as part of multi-line comment handling.
15 : Adding Non-comment Character
} else {
If the current character is not part of a comment, adds it to the result string 'res'.
16 : Accumulating Result
res += str[j];
Adds the current character to the 'res' string.
17 : If In Multi-line Comment
} else {
Handles the case where the current character is inside a multi-line comment.
18 : Handling Multi-line Comment End
if(j < str.size() - 1 && str[j] == '*' && str[j + 1] == '/') {
Checks for the end of a multi-line comment ('*/').
19 : Exiting Multi-line Comment
is_mul = false;
Marks the end of a multi-line comment.
20 : Skipping Character in Multi-line Comment End
j++;
Skips the next character in the string after detecting the end of a multi-line comment.
21 : End of If-Else
}
Ends the conditional check for the multi-line comment.
22 : Return Result
if(!is_mul && res != "") ans.push_back(res);
Adds the processed string (without comments) to the result vector if it is not empty.
23 : Final Return
return ans;
Returns the final result vector with all comments removed.
24 : Function End
}
Ends the function definition.
Best Case: O(n), where n is the total number of characters in the input.
Average Case: O(n), where n is the total number of characters in the input.
Worst Case: O(n), where n is the total number of characters in the input.
Description: The time complexity is linear relative to the size of the input.
Best Case: O(n), where n is the total number of characters in the input.
Worst Case: O(n), where n is the total number of characters in the input.
Description: The space complexity is also linear, as we store the cleaned lines in a separate array.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus