1
class Solution {
2
public:
3
vector<string> removeComments(vector<string> &source) {
4
string cur = "";
5
vector<string> out;
6
bool comm = false; // it will help us see whether comment is active or not
7

8
for (string s : source) {
9
for (int j = 0; j < s.size(); j++) {
10
// that means a comment is started
11
if (!comm && s[j] == '/' && s[j + 1] == '*') {
12
comm = true;
13
j++;
14
}
15
// what if a // <- is present, then full line is not read & break
16
// also make sure it is outside comments
17
else if (!comm && s[j] == '/' && s[j + 1] == '/')
18
break;
19
// check if a comment is completed
20
else if (comm && s[j] == '*' && s[j + 1] == '/') {
21
comm = false;
22
j++;
23
}
24
// or else it is a valid code
25
else if (!comm)
26
cur += s[j];
27
}
28
// now check if it's a comment or not and its not blank (because comments
29
// are blank lines) and add it to output
30

31
if (!comm && cur.size()) {
32
out.push_back(cur);
33
cur = "";
34
}
35
}
36
return out;
37
}
38
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0