Problem Β· String
Table of Contents π
Learn this problemProblem statement
Create a table of contents for a simple markup language. It must follow two rules:
# followed by a space, then it's a chapter title.# 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
tableOfContents(text: String[]) β String[]
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
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# or with ##, these special characters are always followed by a space.More IBM problems
- Parent Process NumberOA Β· Seen Jul 2026
- Request Retry CountOA Β· Seen Jul 2026
- Count Strictly Increasing Subsequences of Length 3OA Β· Seen Jul 2026
- Maximum Requests in a Time WindowOA Β· Seen Jul 2026
- Query Type Frequency WindowOA Β· Seen Jul 2026
- Minimum Number of Non-Empty Disjoint SegmentsOA Β· Seen Jul 2026
- Spam Text ClassificationOA Β· Seen Jul 2026
- Count Ideal NumbersOA Β· Seen Jun 2026