ans = list() i = pt = 0 while i < n: while pt < m and indices[ops[pt]] < i: pt += 1 succeed = False while pt < m and indices[ops[pt]] == i: if s[i:i + len(sources[ops[pt]])] == sources[ops[pt]]: succeed = True break pt += 1 if succeed: ans.append(targets[ops[pt]]) i += len(sources[ops[pt]]) else: ans.append(s[i]) i += 1
var findReplaceString = function(s, indices, sources, targets) { const n = s.length; const m = indices.length;
const ops = newArray(m); for (let i = 0; i < m; i++) { ops[i] = i; } ops.sort((i, j) => indices[i] - indices[j]);
let ans = ""; let pt = 0; for (let i = 0; i < n; ) { while (pt < m && indices[ops[pt]] < i) { pt++; } let found = false; while (pt < m && indices[ops[pt]] == i) { if (s.substring(i, i + sources[ops[pt]].length) === sources[ops[pt]]) { found = true; break; } pt++; } if (found) { ans += targets[ops[pt]]; i += sources[ops[pt]].length; } else { ans += s[i]; i++; } } return ans; };
char * findReplaceString(char * s, int* indices, int indicesSize, char ** sources, int sourcesSize, char ** targets, int targetsSize) { int n = strlen(s), m = indicesSize;
int ops[m][2]; int sourcesLen[sourcesSize]; for (int i = 0; i < m; i++) { ops[i][0] = i; ops[i][1] = indices[i]; } for (int i = 0; i < sourcesSize; i++) { sourcesLen[i] = strlen(sources[i]); } qsort(ops, m, sizeof(ops[0]), cmp);
char *ans = (char *)calloc(1024, sizeof(char)); int pt = 0, pos = 0; for (int i = 0; i < n;) { while (pt < m && indices[ops[pt][0]] < i) { ++pt; } bool succeed = false; while (pt < m && indices[ops[pt][0]] == i) { if (strncmp(s + i, sources[ops[pt][0]], sourcesLen[ops[pt][0]]) == 0) { succeed = true; break; } ++pt; } if (succeed) { pos += sprintf(ans + pos, "%s", targets[ops[pt][0]]); i += sourcesLen[ops[pt][0]]; } else { ans[pos++] = s[i]; ++i; } } return ans; }
funcfindReplaceString(s string, indices []int, sources []string, targets []string)string { n, m := len(s), len(indices)
ops := make([][]int, m) for i := 0; i < m; i++ { ops[i] = []int{i, indices[i]} } sort.Slice(ops, func(i int, j int)bool { return ops[i][1] < ops[j][1] })
ans := "" pt := 0 for i := 0; i < n; { for pt < m && indices[ops[pt][0]] < i { pt++ } succeed := false for pt < m && indices[ops[pt][0]] == i { if s[i : i + min(len(sources[ops[pt][0]]), n - i)] == sources[ops[pt][0]] { succeed = true break } pt++ } if succeed { ans += targets[ops[pt][0]] i += len(sources[ops[pt][0]]) } else { ans += string(s[i]) i++ } } return ans }
funcmin(x int, y int)int { if x > y { return y } return x }
复杂度分析
时间复杂度:O(n + m \log m + ml),其中 n 是字符串 s 的长度,m 是数组 indices 的长度,l 是数组 sources 和 targets 中字符串的平均长度。
publicclassSolution { publicstringFindReplaceString(string s, int[] indices, string[] sources, string[] targets) { int n = s.Length, m = indices.Length;
IDictionary<int, IList<int>> ops = new Dictionary<int, IList<int>>(); for (int i = 0; i < m; ++i) { ops.TryAdd(indices[i], new List<int>()); ops[indices[i]].Add(i); }
StringBuilder ans = new StringBuilder(); for (int i = 0; i < n;) { bool succeed = false; if (ops.ContainsKey(i)) { foreach (int pt in ops[i]) { if (s.Substring(i, Math.Min(i + sources[pt].Length, n) - i).Equals(sources[pt])) { succeed = true; ans.Append(targets[pt]); i += sources[pt].Length; break; } } } if (!succeed) { ans.Append(s[i]); ++i; } } return ans.ToString(); } }
classSolution: deffindReplaceString(self, s: str, indices: List[int], sources: List[str], targets: List[str]) -> str: n, m = len(s), len(indices)
ops = defaultdict(list) for i, idx inenumerate(indices): ops[idx].append(i)
ans = list() i = 0 while i < n: succeed = False if i in ops: for pt in ops[i]: if s[i:i+len(sources[pt])] == sources[pt]: succeed = True ans.append(targets[pt]) i += len(sources[pt]) break ifnot succeed: ans.append(s[i]) i += 1
var findReplaceString = function(s, indices, sources, targets) { const n = s.length, m = indices.length;
const ops = newMap(); for (let i = 0; i < m; i++) { if (!ops.has(indices[i])) { ops.set(indices[i], []); } ops.get(indices[i]).push(i); }
let ans = ""; for (let i = 0; i < n; ) { let succeed = false; if (ops.has(i)) { console.log(ops.get(i)) for (const pt of ops.get(i)) { if (s.substring(i, Math.min(i + sources[pt].length, n)) === sources[pt]) { succeed = true; ans += targets[pt]; i += sources[pt].length; break; } } } if (!succeed) { ans += s[i]; ++i; } } return ans; };
char * findReplaceString(char * s, int* indices, int indicesSize, char ** sources, int sourcesSize, char ** targets, int targetsSize) { int n = strlen(s), m = indicesSize;
HashItem *ops = NULL; for (int i = 0; i < m; ++i) { hashAddItem(&ops, indices[i], i); }
char *ans = (char *)calloc(n * 50 + 1, sizeof(char)); int pos = 0; for (int i = 0; i < n;) { bool succeed = false; HashItem *pEntry = hashFindItem(&ops, i); if (pEntry) { for (struct ListNode *node = pEntry->list; node; node = node->next) { int pt = node->val; if (!strncmp(s + i, sources[pt], strlen(sources[pt]))) { succeed = true; pos += sprintf(ans + pos, "%s", targets[pt]); i += strlen(sources[pt]); break; } } } if (!succeed) { ans[pos++] = s[i++]; } } hashFree(&ops); return ans; }
funcfindReplaceString(s string, indices []int, sources []string, targets []string)string { n, m := len(s), len(indices)
ops := map[int][]int{} for i := 0; i < m; i++ { ops[indices[i]] = append(ops[i], i) }
ans := "" for i := 0; i < n; { succeed := false _, ok := ops[i] if ok { for _, pt := range ops[i] { if s[i : i + min(len(sources[pt]), n - i)] == sources[pt] { succeed = true ans += targets[pt] i += len(sources[pt]) break } } } if !succeed { ans += string(s[i]) i++ } } return ans }
funcmin(x int, y int)int { if x > y { return y } return x }
复杂度分析
时间复杂度:O(n + ml),其中 n 是字符串 s 的长度,m 是数组 indices 的长度,l 是数组 sources 和 targets 中字符串的平均长度。