|
| 1 | +## 1. Mark Bold Characters |
| 2 | + |
| 3 | +::tabs-start |
| 4 | + |
| 5 | +```python |
| 6 | +class Solution: |
| 7 | + def addBoldTag(self, s: str, words: List[str]) -> str: |
| 8 | + n = len(s) |
| 9 | + bold = [False] * n |
| 10 | + |
| 11 | + for word in words: |
| 12 | + start = s.find(word) |
| 13 | + while start != -1: |
| 14 | + for i in range(start, start + len(word)): |
| 15 | + bold[i] = True |
| 16 | + |
| 17 | + start = s.find(word, start + 1) |
| 18 | + |
| 19 | + open_tag = "<b>" |
| 20 | + close_tag = "</b>" |
| 21 | + ans = [] |
| 22 | + |
| 23 | + for i in range(n): |
| 24 | + if bold[i] and (i == 0 or not bold[i - 1]): |
| 25 | + ans.append(open_tag) |
| 26 | + |
| 27 | + ans.append(s[i]) |
| 28 | + |
| 29 | + if bold[i] and (i == n - 1 or not bold[i + 1]): |
| 30 | + ans.append(close_tag) |
| 31 | + |
| 32 | + return "".join(ans) |
| 33 | +``` |
| 34 | + |
| 35 | +```java |
| 36 | +class Solution { |
| 37 | + public String addBoldTag(String s, String[] words) { |
| 38 | + int n = s.length(); |
| 39 | + boolean[] bold = new boolean[n]; |
| 40 | + |
| 41 | + for (String word: words) { |
| 42 | + int start = s.indexOf(word); |
| 43 | + while (start != -1) { |
| 44 | + for (int i = start; i < start + word.length(); i++) { |
| 45 | + bold[i] = true; |
| 46 | + } |
| 47 | + |
| 48 | + start = s.indexOf(word, start + 1); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + String openTag = "<b>"; |
| 53 | + String closeTag = "</b>"; |
| 54 | + StringBuilder ans = new StringBuilder(); |
| 55 | + |
| 56 | + for (int i = 0; i < n; i++) { |
| 57 | + if (bold[i] && (i == 0 || !bold[i - 1])) { |
| 58 | + ans.append(openTag); |
| 59 | + } |
| 60 | + |
| 61 | + ans.append(s.charAt(i)); |
| 62 | + |
| 63 | + if (bold[i] && (i == n - 1 || !bold[i + 1])) { |
| 64 | + ans.append(closeTag); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return ans.toString(); |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +```cpp |
| 74 | +class Solution { |
| 75 | +public: |
| 76 | + string addBoldTag(string s, vector<string>& words) { |
| 77 | + int n = s.size(); |
| 78 | + vector<bool> bold(n); |
| 79 | + |
| 80 | + for (string word: words) { |
| 81 | + int start = s.find(word); |
| 82 | + while (start != -1) { |
| 83 | + for (int i = start; i < start + word.size(); i++) { |
| 84 | + bold[i] = true; |
| 85 | + } |
| 86 | + |
| 87 | + start = s.find(word, start + 1); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + string openTag = "<b>"; |
| 92 | + string closeTag = "</b>"; |
| 93 | + string ans = ""; |
| 94 | + |
| 95 | + for (int i = 0; i < n; i++) { |
| 96 | + if (bold[i] && (i == 0 || !bold[i - 1])) { |
| 97 | + ans += openTag; |
| 98 | + } |
| 99 | + |
| 100 | + ans += s[i]; |
| 101 | + |
| 102 | + if (bold[i] && (i == n - 1 || !bold[i + 1])) { |
| 103 | + ans += closeTag; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return ans; |
| 108 | + } |
| 109 | +}; |
| 110 | +``` |
| 111 | +
|
| 112 | +```javascript |
| 113 | +class Solution { |
| 114 | + /** |
| 115 | + * @param {string} s |
| 116 | + * @param {string[]} words |
| 117 | + * @return {string} |
| 118 | + */ |
| 119 | + addBoldTag(s, words) { |
| 120 | + const n = s.length; |
| 121 | + const bold = new Array(n).fill(false); |
| 122 | + |
| 123 | + for (const word of words) { |
| 124 | + let start = s.indexOf(word); |
| 125 | + while (start !== -1) { |
| 126 | + for (let i = start; i < start + word.length; i++) { |
| 127 | + bold[i] = true; |
| 128 | + } |
| 129 | + start = s.indexOf(word, start + 1); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + const openTag = "<b>"; |
| 134 | + const closeTag = "</b>"; |
| 135 | + const ans = []; |
| 136 | + |
| 137 | + for (let i = 0; i < n; i++) { |
| 138 | + if (bold[i] && (i === 0 || !bold[i - 1])) { |
| 139 | + ans.push(openTag); |
| 140 | + } |
| 141 | + ans.push(s[i]); |
| 142 | + if (bold[i] && (i === n - 1 || !bold[i + 1])) { |
| 143 | + ans.push(closeTag); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return ans.join(""); |
| 148 | + } |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +::tabs-end |
| 153 | + |
| 154 | +### Time & Space Complexity |
| 155 | +The time complexity may differ between languages. It is dependent on how the built-in method is implemented. |
| 156 | +For this analysis, we will assume that we are using Java. |
| 157 | + |
| 158 | +- Time complexity: $O(m \cdot (n^2 \cdot k - n \cdot k^2))$ |
| 159 | +- Space complexity: $O(n)$ |
| 160 | + |
| 161 | +> Where $n$ is `s.length`, $m$ is `words.length`, and $k$ is the average length of the words. |
0 commit comments