MockInterview:722. Remove Comments
Medium
Given a C++ program, remove comments from it. The program
source is an array where source[i] is the i-th line of the source code. This represents the result of splitting the original source code string by the newline character \n.
In C++, there are two types of comments, line comments, and block comments.
The string
// denotes a line comment, which represents that it and rest of the characters to the right of it in the same line should be ignored.
The string
/* denotes a block comment, which represents that all characters until the next (non-overlapping) occurrence of */ should be ignored. (Here, occurrences happen in reading order: line by line from left to right.) To be clear, the string /*/ does not yet end the block comment, as the ending would be overlapping the beginning.
The first effective comment takes precedence over others: if the string
// occurs in a block comment, it is ignored. Similarly, if the string /*occurs in a line or block comment, it is also ignored.
If a certain line of code is empty after removing comments, you must not output that line: each string in the answer list will be non-empty.
There will be no control characters, single quote, or double quote characters. For example,
source = "string s = "/* Not a comment. */";"will not be a test case. (Also, nothing else such as defines or macros will interfere with the comments.)
It is guaranteed that every open block comment will eventually be closed, so
/* outside of a line or block comment always starts a new comment.
Finally, implicit newline characters can be deleted by block comments. Please see the examples below for details.
After removing the comments from the source code, return the source code in the same format.
Example 1:
Input: source = ["/*Test program */", "int main()", "{ ", " // variable declaration ", "int a, b, c;", "/* This is a test", " multiline ", " comment for ", " testing */", "a = b + c;", "}"] The line by line code is visualized as below: /*Test program */ int main() { // variable declaration int a, b, c; /* This is a test multiline comment for testing */ a = b + c; } Output: ["int main()","{ "," ","int a, b, c;","a = b + c;","}"] The line by line code is visualized as below: int main() { int a, b, c; a = b + c; } Explanation: The string/*denotes a block comment, including line 1 and lines 6-9. The string//denotes line 4 as comments.
Example 2:
Input: source = ["a/*comment", "line", "more_comment*/b"] Output: ["ab"] Explanation: The original source string is "a/*comment\nline\nmore_comment*/b", where we have bolded the newline characters. After deletion, the implicit newline characters are deleted, leaving the string "ab", which when delimited by newline characters becomes ["ab"].
Wrong Code:
don't parse the code one character by one character
class Solution {
public List<String> removeComments(String[] source) {
boolean IgnoreLine = false;
boolean startBlockComment = false;
//boolean endComment = false;
List<String> res = new ArrayList<String>();
for(int i = 0; i < source.length; i++){
String line = source[i];
//line = line.trim();
if(!startBlockComment){
int start = line.indexOf("/*");
if(start >= 0){
while(start >= 0){
startBlockComment = true;
int end = line.indexOf("*/");
if(end >= 0){
line = line.substring(0,start)+line.substring(end+2,line.length());
startBlockComment = false;
}else{
line = line.substring(0,start);
}
start = line.indexOf("/*");
}
if(!line.trim().equals("")){
res.add(line);
}
}else{
start = line.indexOf("//");
if(start >= 0){
line = line.substring(0,start);
//if(!line.trim().equals("")){
res.add(line);
//}
}else{
if(!line.trim().equals("")){
res.add(line);
}
}
}
}else{
int end = line.indexOf("*/");
if(end >= 0){
line = line.substring(end+2,line.length());
startBlockComment = false;
if(!line.trim().equals("")){
res.add(line);
}
}
}
}
return res;
}
}
correct code:
parse the whole code one line by one line, parse one line one character by one character. There are two loops.
StringBuilder sb stores the characters which need to output for each line. when each generated line is added to the result List, the stringBuilder need to renew.
startBlockComment means the block comment mode is on.
1) when we see '/*', we start block comment mode, we keep on ignoring the following characters until we see */. it's possible /* and */ at the same line, so you can't skip. you need to continue scanning the character and ignore the character.
2) when we see '//', we need to ignore the remaining character at the same line. so we break; but we need to add whatever we seen into the result. so at the end of each line loop, we need to add generated line into result. and start a new Stringbuilder for next line.
3) when we see */, we turn off the block comment mode and continue scanning the character.
4) at the end of line loop, if the block comment mode is off and output StringBuilder is not empty, add the line into the result.
class Solution {
public List<String> removeComments(String[] source) {
List<String> res = new ArrayList<String>();
StringBuilder sb = new StringBuilder();
boolean startBlockComment = false;
for(int i = 0; i < source.length; i++){
String line = source[i];
for(int j = 0; j < line.length(); j++){
char c = line.charAt(j);
if(!startBlockComment){
if(c == '/' && j + 1 < line.length() && line.charAt(j+1) == '*'){
startBlockComment = true;
j++;//skip two characters
}else if(c == '/' && j + 1 < line.length() && line.charAt(j+1) == '/'){
break;
}else{
sb.append(c);
}
}else{
if(c == '*' && j + 1 < line.length() && line.charAt(j+1) == '/'){
startBlockComment = false;
j++;//skip two characters
}else{
//empty,no need add this character to output
}
}
}
if(!startBlockComment&&sb.length()>0){
res.add(sb.toString());
sb = new StringBuilder();
}
}
return res;
}
}
Comments
Post a Comment