Table of Contents π
Problem statement
Create a table of contents for a simple markup language. It must follow two rules:
- If a line starts with a single
#followed by a space, then it's a chapter title. - If a line starts with a double
#followed by a space, then it's a section title.
The table of contents should be displayed in the following format:
Note that each number is followed by a period and the last period is followed by 1 space.
Complete the function tableOfContents in the editor.
tableOfContents has the following parameter:
string text[n]: the input text
string[]: each string is a line in the table of contents
Function
tableOfContents(text: String[]) β String[]Examples
Example 1
text = ["# Cars", "Cars came into global use during the 20th century", "Most definitions of car say they run primarily on roads", "## Sedan", "Sedan's first recorded use as a name for a car body was in 1912", "## Coupe", "Coupe is a passenger car with a sloping rear roofline and generally two doors", "## SUV", "The predecessors to SUVs date back to military and low-volume models from the late 1930s", "There is no commonly agreed definition of an SUV, and usage varies between countries."]return = ["1. Cars", "1.1. Sedan", "1.2. Coupe", "1.3. SUV"]
The first line of input indicates there are n = 10 lines of text. There is only 1 chapter in the input, and it contains 3 sections. All the lines that don't begin with # or ## are ignored in the table of contents.
Constraints
1 β€ n β€ 10001 β€ length of text[i] β€ 100- When a line starts with
#or with##, these special characters are always followed by a space. - The first line of the text is guaranteed to be a chapter line.