You are given a 0-indexed string
s
that you must perform k
replacement operations on. The replacement operations are given as three 0-indexed parallel arrays, indices
, sources
, and targets
, all of length k
.To complete the
i
th
replacement operation:- Check if the substring
sources[i]
occurs at indexindices[i]
in the original strings
.
- If it does not occur, do nothing.
- Otherwise if it does occur, replace that substring with
targets[i]
.
For example, if
s = "
ab
cd"
, indices[i] = 0
, sources[i] = "ab"
, and targets[i] = "eee"
, then the result of this replacement will be "
eee
cd"
.All replacement operations must occur simultaneously, meaning the replacement operations should not affect the indexing of each other. The testcases will be generated such that the replacements will not overlap.
- For example, a testcase with
s = "abc"
,indices = [0, 1]
, andsources = ["ab","bc"]
will not be generated because the"ab"
and"bc"
replacements overlap.
Return the resulting string after performing all replacement operations on
s
.A substring is a contiguous sequence of characters in a string.

Constraints:
1 <= s.length <= 1000
k == indices.length == sources.length == targets.length
1 <= k <= 100
0 <= indexes[i] < s.length
1 <= sources[i].length, targets[i].length <= 50
s
consists of only lowercase English letters.
sources[i]
andtargets[i]
consist of only lowercase English letters.
풀이
은찬
const findReplaceString = (s, indices, sources, targets) => { const answer = []; const indiceMap = new Map(); let idx = 0; for(let i = 0; i < indices.length; i++){ indiceMap.set(indices[i], [sources[i], targets[i]]); } indices.sort((a, b) => a - b); for(let i = 0; i < s.length; i++){ console.log(i); if(indiceMap.has(i)){ const [source, target] = indiceMap.get(i); const sLen = source.length; const origin = s.slice(i, i + sLen); console.log(origin, source, target) if(origin === source){ answer.push(target); } else{ answer.push(origin); } idx++; i += sLen - 1; } else{ answer.push(s[i]); } } return answer.join(""); };
효성
var findReplaceString = function(s, indices, sources, targets) { let answer = s.split(''); for(let i=0; i<indices.length; i++) { const source = sources[i]; const target = targets[i]; if(s.slice(indices[i], indices[i] + source.length) === source) { answer[indices[i]] = [target]; let cntReplaceWord = source.length - 1; while(cntReplaceWord > 0) { answer[++indices[i]] = ''; cntReplaceWord--; } } } return answer.join(''); };
재영
const zipArrs = (indices, sources, targets) => { const arr = Array(indices.length).fill([]); return arr.map((_, i) => [indices[i], sources[i], targets[i]]); }; const findReplaceString = (s, indices, sources, targets) => { let result = ""; const infos = zipArrs(indices, sources, targets); const infosLength = infos.length; infos.sort((a, b) => a[0] - b[0]); let start = infos[0][0] !== 0 ? s.slice(0, infos[0][0]) : ""; let end = infos[infosLength - 1][0] + infos[infosLength - 1][1].length !== infosLength - 1 ? s.slice(infos[infosLength - 1][0] + infos[infosLength - 1][1].length) : ""; while (infos.length) { const [nowIndex, nowSource, nowTarget] = infos.pop(); const nowWord = s.slice(nowIndex, nowIndex + nowSource.length); result = nowWord === nowSource ? nowTarget + result : nowWord + result; if (infos.length) { const [nextIndex, nextSource, _] = infos[infos.length - 1]; const constantWord = s.slice(nextIndex + nextSource.length, nowIndex); result = constantWord + result; } } return start + result + end; };