Problem solving/Algorithms

[LeetCode] 1528. Shuffle String (Python)

Young_A 2021. 1. 10. 09:44

LeetCode - Problems - Algorithms - 1528. Shuffle String

Problem Description

Given a string s and an integer indices of the same length.

The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.

Return the shuffled string.

 

Example:

Constraints:

My Solution (Python)

class Solution(object):
    def restoreString(self, s, indices):
        """
        :type s: str
        :type indices: List[int]
        :rtype: str
        """
        answer = list(s)
        
        index = 0
        for i in indices:
            answer[i] = s[index]
            index += 1

        return "".join(answer)