Problem Statement
More formally we can define an MLC as follows:
- A <Nester> is a string, possibly empty, that contains no "/*" and no "*/" OR a Nester consists of three concatenated strings, <Nester> <MLC> <Nester>
- An <MLC> is a string that consists of three concatenated substrings, "/*" + <Nester> + "*/" (Notice that a <Nester> can be an empty string).
Create a class CommentNest that contains a method whatsLeft that takes a String[] lines (the lines of a program) as input and returns the number of characters that remain after the MLC's are removed. Each element of lines (including the last) is followed by an end-of-line character; the return should count both the remaining characters from lines and the remaining end-of-line characters.
Definition
- Class:
- CommentNest
- Method:
- whatsLeft
- Parameters:
- String[]
- Returns:
- int
- Method signature:
- int whatsLeft(String[] lines)
- (be sure your method is public)
Constraints
- lines contains between 1 and 20 elements, inclusive.
- Each element of lines contains between 0 and 50 characters, inclusive.
- Each character in each element of lines is either '/', '*', or a lowercase letter ('a'-'z').
Examples
{"abc","def"}Returns: 8
Nothing is removed. There are 6 characters plus 2 end-of-lines.{"a//bc/*/d", "", "x/*/b"}Returns: 7
/*/d@@x/*/ is an MLC (where @ denotes end-of-line), since /d@@x/ is a Nester. That leaves 6 characters from the lines plus 1 end-of-line character.{"a/***b///**/*/"}Returns: 2
/***b///**/*/ is an MLC, since its **b///**/ is a Nester since it consists of **b// + /**/ + <empty> which is Nester MLC Nester.{"/*/*/abc//*xyz*/*" }Returns: 6
/*/*/ is an MLC. No longer MLC starts with the first /*. Continuing, we look for an MLC that starts at the a, at the b, at the c, at the /. When we get to the next /, we find /*xyz*/ is the longest MLC that starts there. We continue at the final * which cannot begin a comment. After all the comments we found have been removed, we are left with abc/ and *@ which is a total of 6 characters.{"/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*", "/*abc*/"}Returns: 3
The MLC that starts at the first character includes all but the final * from the first line. It contains the character '/' nested 12 deep in comments.{"*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/", "*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/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