-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAddBoldTag.cpp
64 lines (64 loc) · 1.5 KB
/
AddBoldTag.cpp
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
class Solution {
public:
string boldWords(vector<string>& words, string S) {
int i,j,sz=words.size(),len=S.length(),start,end,k,len1;
vector<int>flag(len+1,0);
string temp;
for(i=0;i<sz;i++)
{
temp=words[i];
len1=temp.length();
for(j=0;j<len;j++)
{
if(S.find(temp,j)!=string::npos)
{
start=S.find(temp,j);
end=start+len1-1;
for(k=start;k<=end;k++)
flag[k]=1;
}
else
break;
}
}
int status=0;
temp="";
for(i=0;i<len;i++)
{
if((flag[i]==1) && (status==0))
{
temp=temp+"<b>";
status=1;
}
else if((status==1) && (flag[i]==0))
{
temp=temp+"</b>";
status=0;
}
temp=temp+S[i];
}
if(status)
temp=temp+"</b>";
/*
i=0;
while(i<len)
{
if(flag[i]==0)
{
temp=temp+s[i];
i++;
}
else
{
temp=temp+"<b>";
j=i;
while((j<len) && (flag[j]==1))
temp=temp+s[j++];
temp=temp+"</b>";
i=j;
}
}
*/
return temp;
}
};