FastPrepFastPrep
Problem Brief

Table of Contents πŸ€

FULLTIMEOA
See IBM online assessment and hiring insights

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.

    Function Description

    Complete the function tableOfContents in the editor.

    tableOfContents has the following parameter:

    • string text[n]: the input text

    Returns

    string[]: each string is a line in the table of contents

    1Example 1

    Input
    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."]
    Output
    ["1. Cars", "1.1. Sedan", "1.2. Coupe", "1.3. SUV"]
    Explanation
    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

    Limits and guarantees your solution can rely on.

  • 1 ≀ n ≀ 1000
  • 1 ≀ 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.
  • public String[] tableOfContents(String[] text) {
        // write your code here
    }
    
    Input

    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."]

    Output

    ["1. Cars", "1.1. Sedan", "1.2. Coupe", "1.3. SUV"]

    Sign in to submit your solution.