LC: 616. Add Bold Tag in String
https://leetcode.com/problems/add-bold-tag-in-string/
616. Add Bold Tag in String
You are given a string s and an array of strings words. You should add a closed pair of bold tag <b> and </b> to wrap the substrings in s that exist in words. If two such substrings overlap, you should wrap them together with only one pair of closed bold-tag. If two substrings wrapped by bold tags are consecutive, you should combine them.
Return s after adding the bold tags.
Example 1:
Input: s = "abcxyz123", words = ["abc","123"]
Output: "<b>abc</b>xyz<b>123</b>"Example 2:
Input: s = "aaabbcc", words = ["aaa","aab","bc"]
Output: "<b>aaabbc</b>c"Constraints:
1 <= s.length <= 10000 <= words.length <= 1001 <= words[i].length <= 1000sandwords[i]consist of English letters and digits.All the values of
wordsare unique.
Note: This question is the same as 758: https://leetcode.com/problems/bold-words-in-string/
The Essence:
Wir können alle fettgedruckte Zeichen in einem Boolean-Array zeigen. Dafür sollen wir in dem gegebenen String alle Teilstrings aus dem gegebenen Wörterbuch finden. Nachdem man das Boolean-Array schafft, dann kann die neue Zeichenfolge durch Zufügung des Bold-Tags in Indizes, wo der Wert true anfängt oder endet, gebildet werden.
Details:
Um die entsprechenden Teilzeichenfolgen aus dem Wörterbuch zu finden, gibt es viele mögliche Ansätze. Dazu gehört die einfache Verwendung der Bestimmung jedes Wortes mithilfe des Knuth-Morris-Pratt-Algorithmus, eingebauter Methoden der Programmiersprachen, oder komplexerer Algorithmen wie der Aho-Corasick-Algorithmus.
Solution(s) and Further Explanation:
Default Code:
Last updated