-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddBoldTagInString616.cpp
More file actions
77 lines (71 loc) · 2.13 KB
/
addBoldTagInString616.cpp
File metadata and controls
77 lines (71 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
Given a string s and a list of strings dict, you need to add a closed pair of bold tag <b> and </b> to wrap the substrings in s that exist in dict. If two such substrings overlap, you need to wrap them together by only one pair of closed bold tag. Also, if two substrings wrapped by bold tags are consecutive, you need to combine them.
Example 1:
Input:
s = "abcxyz123"
dict = ["abc","123"]
Output:
"<b>abc</b>xyz<b>123</b>"
Example 2:
Input:
s = "aaabbcc"
dict = ["aaa","aab","bc"]
Output:
"<b>aaabbc</b>c"
Note:
The given dict won't contain duplicates, and its length won't exceed 100.
All the strings in input have length in range [1, 1000].
*/
class Solution {
public:
string addBoldTag(string s, vector<string>& dict) {
vector<bool> isBold(s.size());
for (string word: dict) {
std::size_t found = s.find(word);
while (found != string::npos) {
for (size_t i = found; i < found + word.size(); ++i) {
isBold[i] = true;
}
found = s.find(word, found+1);
}
}
string boldS;
for (size_t i = 0; i < s.size(); ++i) {
if (isBold[i] == true && (i == 0 || isBold[i-1] == false)) {
boldS.append("<b>");
}
boldS.push_back(s[i]);
if (isBold[i] == true && (i == s.size()-1 || isBold[i+1] == false)){
boldS.append("</b>");
}
}
return boldS;
}
};
//the fatest method
class Solution {
public:
string addBoldTag(string s, vector<string>& dict) {
int n=s.size();
vector<int> mark(n, 0);
for(auto str: dict){
int p=-1;
while(true){
p=s.find(str, p+1);
if(p==-1) break;
for(int i=p; i<p+str.size(); i++) mark[i]=1;
}
}
string res;
int i=0;
while(i<n){
if(mark[i]){
res+="<b>";
while(i<n && mark[i]) res+=s[i++];
res+="</b>";
}
else res+=s[i++];
}
return res;
}
};