Practice Remove Comments
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Remove comments from the C++ program supplied as an array of source lines. Outside a comment, // discards the rest of that line, while /* starts a block ending at the next non-overlapping */. Comment-opening delimiters inside an active comment have no effect; in particular, /*/ does not close a block. Block comments may span lines, and removing one can join the text before and after it into one output line. Every opened block is guaranteed to close. Inputs contain no quotes or control characters, and defines or macros do not change comment parsing. Preserve all other characters and return the remaining lines, omitting lines that contain no characters.
Source: Remove Comments on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
source = ["/*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;","}"]# Example 2
source = ["a/*comment", "line", "more_comment*/b"]
["ab"]
Constraints
Related LeetCode Problems
Practice more problems similar to Remove Comments.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.