@@ -0,0 +1,1885 @@
+// Time complexity is O(N * M), where N is the number of lines and M is the max length of a line.%0A// String builder append is an amortised O(1) operation as well as appending to the list.%0A// Space complexity is O(N * M), we need to store the entire input when there are no comments.%0Aclass Solution %7B%0A  public List%3CString%3E removeComments(String%5B%5D source) %7B%0A    ArrayList%3CString%3E lines = new ArrayList%3CString%3E();%0A    boolean multiline = false;%0A    StringBuilder sb = new StringBuilder();%0A%0A    for (int i = 0; i %3C source.length; i++) %7B%0A      String line = source%5Bi%5D;%0A      int curr = 0, len = line.length();%0A%0A      while (curr %3C len) %7B%0A        char t = line.charAt(curr);%0A        switch (t) %7B%0A          case '/':%0A            if (!multiline && curr + 1 %3C len && line.charAt(curr + 1) == '/') %7B%0A              if (sb.length() %3E 0) %7B%0A                lines.add(sb.toString());%0A                sb.setLength(0);%0A              %7D%0A              curr = len;%0A            %7D else if (!multiline && curr + 1 %3C len && line.charAt(curr + 1) == '*') %7B%0A              multiline = true;%0A              curr += 2; // skip '/*'%0A            %7D else if (multiline) %7B%0A              curr++;%0A            %7D else %7B%0A              sb.append(t);%0A              curr++;%0A            %7D%0A            break;%0A          case '*':%0A            if (multiline && curr + 1 %3C len && line.charAt(curr + 1) == '/') %7B%0A              curr += 2; // skip '*/'%0A              multiline = false;%0A            %7D else if (multiline) %7B%0A              curr++;%0A            %7D else %7B%0A              sb.append(t);%0A              curr++;%0A            %7D%0A            break;%0A          default:%0A            if (!multiline) %7B%0A              sb.append(t);%0A            %7D%0A            curr++;%0A            break;%0A        %7D%0A      %7D%0A%0A      if (!multiline && sb.length() %3E 0) %7B%0A        lines.add(sb.toString());%0A        sb.setLength(0);%0A      %7D%0A    %7D%0A%0A    return lines;%0A  %7D%0A%7D%0A

add solution