Add Bold Tag in String
Real-world version: Marking Up the Terms in a LineThe statement here is our own: the same inputs, outputs and technique.
A line of text line and a list of terms are given. Wherever a term turns up inside line, that stretch of the line has to be wrapped between the markers <hi> and </hi>.
Stretches may overlap or sit flush against each other. Use as few pairs of markers as possible, so any stretches that overlap or touch end up inside a single pair.
Return the marked-up line.
Examples
Example 1
- Input
- line = "hello world", terms = ["lo", "wor"]
- Output
- "hel<hi>lo</hi> <hi>wor</hi>ld"
One term sits at the fourth letter and the other starts the second word. The two stretches neither touch nor overlap, so each takes its own pair of markers.
Example 2
- Input
- line = "abcdef", terms = ["cd", "de"]
- Output
- "ab<hi>cde</hi>f"
The two stretches share the letter d, so they go inside a single pair of markers rather than two.
Example 3
- Input
- line = "abc", terms = []
- Output
- "abc"
With no terms to look for the line comes back untouched.
Constraints
- 1 <= line.length <= 1000
- 0 <= terms.length <= 100
- 1 <= terms[i].length <= 1000
- line and every term hold only English letters and digits
- no two terms are the same
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def add_bold_tag(line: str, terms: list[str]) -> str:public String addBoldTag(String line, String[] terms)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Word BreakReal-world version: Label Splits Into CodesMedium
- Top K Frequent WordsReal-world version: Most Drawn Part CodesMedium
- Find the Length of the Longest Common PrefixReal-world version: Longest Shared Route LeadMedium
- Shortest Uncommon Substring in an ArrayReal-world version: Unique Fragment Per CodeMedium