HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
👻
개발 기록
/
코딩테스트 스터디
코딩테스트 스터디
/
Find And Replace in String

Find And Replace in String

Link
https://leetcode.com/problems/find-and-replace-in-string/
Deadline
Feb 12, 2022
Status
Archived
Type
Sort
string
Array
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 ith replacement operation:
  1. Check if the substring sources[i] occurs at index indices[i] in the original string s.
  1. If it does not occur, do nothing.
  1. Otherwise if it does occur, replace that substring with targets[i].
For example, if s = "abcd", indices[i] = 0, sources[i] = "ab", and targets[i] = "eee", then the result of this replacement will be "eeecd".
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], and sources = ["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.
notion image
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] and targets[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; };