bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 758. Bold Words in String

758. Bold Words in String

Leetcode Solutions

Marking and Merging Intervals

  1. Initialize an empty list intervals to store the intervals where keywords appear.
  2. For each keyword in words, find all occurrences in s and add the corresponding intervals to intervals.
  3. Sort intervals based on the start index of each interval.
  4. Initialize an empty list merged to store the merged intervals.
  5. Iterate through intervals and merge overlapping or consecutive intervals.
  6. Initialize an empty string result and a variable last to keep track of the end of the last merged interval.
  7. Iterate through merged and construct result by appending non-bold parts of s and the bold parts with tags.
  8. Append any remaining part of s after the last interval to result.
  9. Return result.
UML Thumbnail

Boolean Array for Bold Marking

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...