One pass solution in Java


  • 5
    N

    We only need to check for two things:

    1. If we see '//' we stop reading the current line, and add whatever characters we have seen to the result.
    2. If we see '/*' then we start the multiline comment mode and we keep on ignoring characters until we see '*/'.
    3. If the current character is neither of the above two and the multiline comment mode is off, then we add that character to the current line.

    Once we parse one line (source[i]), then if the mode is off, we add the currently generated line (StringBuilder) to the result and repeat for source[i + 1].

    We need to be careful not to insert empty lines in the result.

    class Solution {
        public List<String> removeComments(String[] source) {
            List<String> res = new ArrayList<>();
            StringBuilder sb = new StringBuilder();     
            boolean mode = false;
            for (String s : source) {
                for (int i = 0; i < s.length(); i++) {
                    if (mode) {
                        if (s.charAt(i) == '*' && i < s.length() - 1 && s.charAt(i + 1) == '/') {
                            mode = false;
                            i++;        //skip '/' on next iteration of i
                        }
                    }
                    else {
                        if (s.charAt(i) == '/' && i < s.length() - 1 && s.charAt(i + 1) == '/') {
                            break;      //ignore remaining characters on line s
                        }
                        else if (s.charAt(i) == '/' && i < s.length() - 1 && s.charAt(i + 1) == '*') {
                            mode = true;
                            i++;           //skip '*' on next iteration of i
                        }
                        else    sb.append(s.charAt(i));     //not a comment
                    }
                }
                if (!mode && sb.length() > 0) {
                    res.add(sb.toString());
                    sb = new StringBuilder();   //reset for next line of source code
                }
            }
            return res;
        }
    }
    

    Thanks to @ihaveayaya for suggestion to remove some duplicate code.


  • 1
    I

    Thanks for sharing this nice and neat solution!
    Just a minor suggestion.
    You can just break in
    if (s.charAt(i) == '/' && i < s.length() - 1 && s.charAt(i + 1) == '/')
    and remove i == s.length() check in if (i == s.length() && !mode && sb.length() > 0)


  • 0
    N

    Thanks for the kind words @ihaveayaya. I have made changes accordingly and also added some comments.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.