package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.com/problems/remove-comments/
 */
public class Problem722 {
    public List<String> removeComments(String[] source) {
        List<String> result = new ArrayList<>();
        boolean skip = false;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < source.length; i++) {
            String src = source[i];
            int j = 0;
            outer:
            for (; j < src.length(); j++) {
                if (j + 1 < src.length() && src.charAt(j) == '/') {
                    if (!skip && src.charAt(j + 1) == '/') {
                        skip = false;
                        break outer;
                    } else if (!skip && src.charAt(j + 1) == '*') {
                        skip = true;
                        j++;
                    }
                } else if (j + 1 < src.length() && src.charAt(j) == '*') {
                    if (skip && src.charAt(j + 1) == '/') {
                        skip = false;
                        j += 2;
                    }
                }
                if (!skip && j < src.length()) {
                    sb.append(src.charAt(j));
                }
            }
            if (sb.length() > 0 && !skip) {
                result.add(sb.toString());
                sb = new StringBuilder();
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Problem722 prob = new Problem722();
        System.out.println(prob.removeComments(new String[]{
            "/*Test program */",
            "int main()",
            "{ ",
            "  // variable declaration ",
            "int a, b, c;",
            "/* This is a test",
            "   multiline  ",
            "   comment for ",
            "   testing */",
            "a = b + c;",
            "}"})); // ["int main()","{ ","  ","int a, b, c;","a = b + c;","}"]
        System.out.println(prob.removeComments(new String[]{
            "a/*comment",
            "line",
            "more_comment*/b"})); // [ab]
        System.out.println(prob.removeComments(new String[]{
            "a/b/*comment*/c+d/*",
            "line",
            "more_comment*/b"})); // [a/bc+db]
        System.out.println(prob.removeComments(new String[]{
            "void func(int k) {",
            "// this function does nothing /*",
            "   k = k*2/4;",
            "   k = k/2;*/", "}"
        })); // ["void func(int k) {","   k = k*2/4;","   k = k/2;*/","}"]
        System.out.println(prob.removeComments(new String[]{
            "a//*b/*/c",
            "blank",
            "d/*/e/*/f"
        })); // ["a","blank","df"]
        System.out.println(prob.removeComments(new String[]{
            "a/*/b//*c",
            "blank",
            "d//*e/*/f"
        })); // ["af"]
    }
}

@@ -843,16 +843,50 @@
    j++;%0A
+                        continue;%0A
         
@@ -891,32 +891,32 @@
               %7D%0A
-
                 
@@ -1101,13 +1101,44 @@
    j
- += 2
+++;%0A                        continue
 ;%0A  
@@ -1447,32 +1447,32 @@
     %7D%0A        %7D%0A
+
         return r
@@ -1487,1452 +1487,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem722 prob = new Problem722();%0A        System.out.println(prob.removeComments(new String%5B%5D%7B%0A            %22/*Test program */%22,%0A            %22int main()%22,%0A            %22%7B %22,%0A            %22  // variable declaration %22,%0A            %22int a, b, c;%22,%0A            %22/* This is a test%22,%0A            %22   multiline  %22,%0A            %22   comment for %22,%0A            %22   testing */%22,%0A            %22a = b + c;%22,%0A            %22%7D%22%7D)); // %5B%22int main()%22,%22%7B %22,%22  %22,%22int a, b, c;%22,%22a = b + c;%22,%22%7D%22%5D%0A        System.out.println(prob.removeComments(new String%5B%5D%7B%0A            %22a/*comment%22,%0A            %22line%22,%0A            %22more_comment*/b%22%7D)); // %5Bab%5D%0A        System.out.println(prob.removeComments(new String%5B%5D%7B%0A            %22a/b/*comment*/c+d/*%22,%0A            %22line%22,%0A            %22more_comment*/b%22%7D)); // %5Ba/bc+db%5D%0A        System.out.println(prob.removeComments(new String%5B%5D%7B%0A            %22void func(int k) %7B%22,%0A            %22// this function does nothing /*%22,%0A            %22   k = k*2/4;%22,%0A            %22   k = k/2;*/%22, %22%7D%22%0A        %7D)); // %5B%22void func(int k) %7B%22,%22   k = k*2/4;%22,%22   k = k/2;*/%22,%22%7D%22%5D%0A        System.out.println(prob.removeComments(new String%5B%5D%7B%0A            %22a//*b/*/c%22,%0A            %22blank%22,%0A            %22d/*/e/*/f%22%0A        %7D)); // %5B%22a%22,%22blank%22,%22df%22%5D%0A        System.out.println(prob.removeComments(new String%5B%5D%7B%0A            %22a/*/b//*c%22,%0A            %22blank%22,%0A            %22d//*e/*/f%22%0A        %7D)); // %5B%22af%22%5D%0A    %7D
 %0A%7D%0A

Solve problem 722