Apply Overlapping HTML Formatting Ranges
Problem statement
Apply formatting ranges to text. Each entry in formats is type:start:end, where type is bold, italic, or underline and [start,end) is a half-open character range. Emit valid HTML using b, i, and u tags. When ranges cross, close and reopen tags as needed. Nest active tags in bold, italic, underline order.
For this exercise, assume the input is literal text. Escape each original &, <, and > as &, <, and >, respectively, while inserting the formatting tags. Escaping does not change the original range indices.
Function
applyHtmlFormats(text: String, formats: String[]) → StringExamples
Example 1
text = "ABCDE"formats = ["bold:0:2","italic:1:3"]return = "<b>A<i>B</i></b><i>C</i>DE"The crossing ranges require italic to close before bold and reopen afterward.
Constraints
1 <= text.length <= 10000.0 <= formats.length <= 10000.- Every range satisfies
0 <= start < end <= text.length. - For this exercise, assume
textcontains ASCII characters and is literal text, not existing HTML; range indices refer to the original characters before escaping.