-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0833-FindAndReplaceInString.cs
43 lines (38 loc) · 1.19 KB
/
0833-FindAndReplaceInString.cs
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
//-----------------------------------------------------------------------------
// Runtime: 104ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
using System.Linq;
using System.Text;
namespace LeetCode
{
public class _0833_FindAndReplaceInString
{
public string FindReplaceString(string S, int[] indexes, string[] sources, string[] targets)
{
var N = S.Length;
var match = new int[N];
for (int i = 0; i < N; i++)
match[i] = -1;
for (int i = 0; i < indexes.Length; i++)
if (S.Substring(indexes[i], sources[i].Length).SequenceEqual(sources[i]))
match[indexes[i]] = i;
var sb = new StringBuilder();
var index = 0;
while (index < N)
{
if (match[index] > -1)
{
sb.Append(targets[match[index]]);
index += sources[match[index]].Length;
}
else
{
sb.Append(S[index++]);
}
}
return sb.ToString();
}
}
}