Image Credits GeeksforGeeks

Remove Comments

Nishant Tanwar 🕉️

--

This is the explanation for the Leetcode question named “Remove Comments”(https://leetcode.com/problems/remove-comments/)

The problem may seems daunting at first and to be honest even I had been avoiding it it for some months now. But decided to give it some time today and sharing my experience. Once you spend enough time on it the logic comes to you as it mostly brute force and does not require prior knowledge of any algorithm or special data structure.

Logic

The problem boils down to removing comments. There are two kinds of comment strategy in C#. One is inline comments using “//” double slash and other is multiline comments using “/*” for opening and “*/” for closing the comments.

The entire logic here resolves around handling the two boolean variables inlineComment and multiLineComment. These two let us know if we are in an active inline comment or an active multiline comment.

We start by iterating over the source array. We further iterate over all the characters contained in each sentence. The question here is if want to remove comments then we need to check our characters to contains either “/*” in sequence or “//” and also track “*/” to keep track where the comment ends.

Pay attention to the first two check statements for single slash and double slash. For multiline comment to start we are making sure we are not already in a multiline comment and not in an inline comment. If not then we start our multiline comment from here and mark our multiline bool to be true. Else we check for double slash. Not checking if already in double slash or multiline comment in this condition as it does not matter even if we are as there is no closing of double slash and the inline comment bool refreshes to false after every sentence. The last condition checks for closing comments in the case if no inline comment is on and there is an existing multi line comment open(these are very important edge cases which are often missed).

If there is no ongoing inline comment or multi line comment then we are free to add the characters to our stringbuilder.

Processing stringbuilder at the end of each sentence if its non empty. The extra condition that multiline comment is not true helps to ignore implicit new line characters when compiling the result.

The Code

public class Solution {
public IList<string> RemoveComments(string[] source)
{
IList<string> result = new List<string>();
if(source == null)
{
return result;
}
bool multilineComment = false;
StringBuilder sb = new StringBuilder();
foreach(string s in source)
{
if(!multilineComment)
{
sb = new StringBuilder();
}
bool inlineComment = false;
int i = 0;
while(i < s.Length)
{
if(i < s.Length-1)
{
if(!inlineComment && s[i] == '/' && s[i+1] == '*' && !multilineComment)
{
multilineComment = true;
i = i + 2;
continue;
}
else if(s[i] == '/' && s[i+1] == '/')
{
if(!multilineComment)
{
inlineComment = true;
i = i + 2;
continue;
}
}
else if(!inlineComment && s[i] == '*' && s[i+1] == '/' && multilineComment)
{
multilineComment = false;
i = i + 2;
continue;
}
}
if(!multilineComment && !inlineComment)
{
sb.Append(s[i]);
}
i++;
}
string newStr = sb.ToString();
if(newStr != string.Empty && !multilineComment)
result.Add(newStr);
}

return result;
}
}

--

--