FastPrepApply Overlapping HTML Formatting Ranges

Apply Overlapping HTML Formatting Ranges

Benchling logoBenchling● MediumNEW GRADPHONE SCREEN
Learn

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 &amp;, &lt;, and &gt;, respectively, while inserting the formatting tags. Escaping does not change the original range indices.

Function

applyHtmlFormats(text: String, formats: String[]) → String

Examples

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 text contains ASCII characters and is literal text, not existing HTML; range indices refer to the original characters before escaping.

More Benchling problems

See Benchling hiring insights
public String applyHtmlFormats(String text, String[] formats) {
    // Write your code here.
}
text"ABCDE"
formats["bold:0:2","italic:1:3"]
expected"<b>A<i>B</i></b><i>C</i>DE"
Checking account…