LeetCode 722: Remove Comments Solution

Master LeetCode problem 722 (Remove Comments), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

722. Remove Comments

Problem Explanation

Explanation:

To remove comments from the given C++ program, we can iterate through each line of the program and process the comments accordingly. We need to handle both line comments (//) and block comments (/* */). We will keep track of whether we are currently inside a block comment or not. If we encounter a line comment, we will ignore the rest of the line. If we encounter the start of a block comment, we will ignore everything until we find the end of that block comment.

  1. Iterate through each line of the program.
  2. Process each character in the line:
    • If not in a block comment:
      • If encountering //, ignore the rest of the line.
      • If encountering /*, set a flag to indicate we are inside a block comment.
    • If in a block comment:
      • If encountering */, reset the flag and continue processing.

Solution Code

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

class Solution {
    public List<String> removeComments(String[] source) {
        List<String> result = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        boolean inBlockComment = false;

        for (String line : source) {
            for (int i = 0; i < line.length(); i++) {
                if (!inBlockComment) {
                    if (i == line.length() - 1) {
                        sb.append(line.charAt(i));
                    } else {
                        String twoChars = line.substring(i, i + 2);
                        if (twoChars.equals("//")) {
                            break;
                        } else if (twoChars.equals("/*")) {
                            inBlockComment = true;
                            i++;
                        } else {
                            sb.append(line.charAt(i));
                        }
                    }
                } else {
                    if (i < line.length() - 1 && line.substring(i, i + 2).equals("*/")) {
                        inBlockComment = false;
                        i++;
                    }
                }
            }

            if (!inBlockComment && sb.length() > 0) {
                result.add(sb.toString());
                sb.setLength(0);
            }
        }

        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 722 (Remove Comments)?

This page provides optimized solutions for LeetCode problem 722 (Remove Comments) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 722 (Remove Comments)?

The time complexity for LeetCode 722 (Remove Comments) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 722 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 722 in Java, C++, or Python.

Back to LeetCode Solutions