[LeetCode] Shuffle String

아르당·2026년 5월 21일

LeetCode

목록 보기
321/569
post-thumbnail

문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음

Problem

문자열 s와 같은 길이의 정수 배열 indices가 주어진다. 문자열 s는 i번째 위치의 문자가 섞인 문자열에서 indices[i]로 이동하도록 섞인다.

섞인 문자열을 반환해라.

Example

#1

Input: s = "codeleet", indices = [4, 5, 6, 7, 0, 2, 1, 3]
Output: "leetcode"
Explanation: "codeleet"는 섞인 후 "leetcode"가 된다.

#2
Input: s = "abc", indices = [0, 1, 2]
Output: "abc"
Explanation: 섞인 후 각 문자들은 그 자리에 있다.

Constraints

  • s.length == indices.length == n
  • 1 <= n <= 100
  • s는 오직 영어 소문자로 구성된다.
  • 0 <= indices[i] < n
  • indices의 모든 값은 고유하다.

Solved

class Solution {
    public String restoreString(String s, int[] indices) {
        int n = s.length();
        char[] c = new char[n];

        for(int i = 0; i < n; i++){
            c[indices[i]] = s.charAt(i);
        }

        return new String(c);
    }
}
profile
내 마음대로 코드 작성하는 세상

0개의 댓글