-
[Programmers] JavaScript 알고리즘 | Lv.0 문자열 여러 번 뒤집기► JS Algorithm/Programmers 2024. 8. 19. 21:21반응형
🔒 문제 설명
문자열 my_string과 이차원 정수 배열 queries가 매개변수로 주어집니다. queries의 원소는 [s, e] 형태로, my_string의 인덱스 s부터 인덱스 e까지를 뒤집으라는 의미입니다. my_string에 queries의 명령을 순서대로 처리한 후의 문자열을 return 하는 solution 함수를 작성해 주세요.
🔒 제한사항
- my_string은 영소문자로만 이루어져 있습니다.
- 1 ≤ my_string의 길이 ≤ 1,000
- queries의 원소는 [s, e]의 형태로 0 ≤ s ≤ e < my_string의 길이를 만족합니다.
- 1 ≤ queries의 길이 ≤ 1,000
🔒 입출력 예
my_string queries result "rermgorpsam" [[2, 3], [0, 7], [5, 9], [6, 10]] "programmers"
🔒 입출력 예 설명
- 예제 1번의 my_string은 "rermgorpsam"이고 주어진 queries를 순서대로 처리하면 다음과 같습니다.
|queries|my_string|
|---|---|
||"rermgorpsam"|
|[2, 3]|"remrgorpsam"|
|[0, 7]|"progrmersam"|
|[5, 9]|"prograsremm"|
|[6, 10]|"programmers"|
따라서 "programmers"를 return 합니다. 🔐 solution of mine
Array.prototype.reduce()
Array.prototype.slice()
Array.prototype.reverse()
Array.prototype.splice()
Array.prototype.splice()
function solution(my_string, queries) { const arr = [...my_string]; queries.reduce((a, [s, e]) => { const reversed = a.slice(s, e + 1).reverse(); a.splice(s, e - s + 1, ...reversed); return a; }, arr); return console.log(arr.join("")); } solution("rermgorpsam", [ [2, 3], [0, 7], [5, 9], [6, 10], ]); // expected output: "programmers"
🔐 GPT's solution
String.prototype.split()
Array.prototype.forEach()
Array.prototype.slice()
Array.prototype.reverse()
Array.prototype.join()
function solution(my_string, queries) { let strArray = my_string.split(""); // 문자열을 배열로 변환하여 작업합니다. queries.forEach(([s, e]) => { // s부터 e까지의 부분을 뒤집고, 배열에 다시 삽입합니다. let reversedPart = strArray.slice(s, e + 1).reverse(); for (let i = s; i <= e; i++) { strArray[i] = reversedPart[i - s]; } }); return console.log(strArray.join("")); // 배열을 다시 문자열로 변환하여 반환합니다. } solution("rermgorpsam", [ [2, 3], [0, 7], [5, 9], [6, 10], ]); // expected output: "programmers"
🔐 solution of others
String.prototype.split()
Array.prototype.forEach()
Array.prototype.slice()
Array.prototype.splice()
Array.prototype.reverse()
Array.prototype.join()
function solution(my_string, queries) { let arr = my_string.split(""); queries.forEach(([s, e]) => { const range = arr.slice(s, e + 1); arr.splice(s, range.length, ...range.reverse()); }); return console.log(arr.join("")); } solution("rermgorpsam", [ [2, 3], [0, 7], [5, 9], [6, 10], ]); // expected output: "programmers"
🔐 solution of others
Array.prototype.forEach()
while
Array.prototype.join()
function solution(my_string, queries) { const arr = [...my_string]; queries.forEach(([s, e]) => { while (s < e) { [arr[s], arr[e]] = [arr[e], arr[s]]; s++; e--; } }); return console.log(arr.join("")); } solution("rermgorpsam", [ [2, 3], [0, 7], [5, 9], [6, 10], ]); // expected output: "programmers"
🔐 solution of others
Array.prototype.reduce()
Array.prototype.slice()
Array.prototype.reverse()
Array.prototype.splice()
Array.from()
Array.prototype.join()
function solution(my_string, queries) { return console.log( queries .reduce((a, [s, e]) => { let range = a.slice(s, e + 1); range.reverse(); a.splice(s, e - s + 1, ...range); return a; }, Array.from(my_string)) .join("") ); } solution("rermgorpsam", [ [2, 3], [0, 7], [5, 9], [6, 10], ]); // expected output: "programmers"
반응형'► JS Algorithm > Programmers' 카테고리의 다른 글
[Programmers] JavaScript 알고리즘 | Lv.0 그림 확대 (0) 2024.08.19 [Programmers] JavaScript 알고리즘 | Lv.0 a와 b 출력하기 (0) 2024.08.19 [Programmers] JavaScript 알고리즘 | Lv.0 문자 개수 세기 (0) 2024.08.18 [Programmers] JavaScript 알고리즘 | Lv.0 문자열 계산하기 (0) 2024.08.18 [Programmers] JavaScript 알고리즘 | Lv.0 삼각형의 완성조건 (2) (0) 2024.08.18