Move Each Character to Its Specified Destination Index

Quick Overview

Move Each Character to Its Specified Destination Index. Character s[i] must move to output position indices[i].

Move Each Character to Its Specified Destination Index

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Character s[i] must move to output position indices[i]. The indices array is a permutation of all valid positions. Return the resulting string. ```cpp class Solution { public: string restoreString(string s, vector<int>& indices); }; ``` ### Examples **Example 1:** ```text Input: s = "abcd", indices = [2,0,3,1] Output: "bdac" Explanation: b goes to0,d to1,a to2,and c to3. ``` **Example 2:** ```text Input: s = "xy", indices = [1,0] Output: "yx" Explanation: The two destination positions swap the characters. ``` ### Constraints - s.length equals indices.length equals n,with `1 <= n <= 100`. - s contains only lowercase English letters. - All indices are unique and lie in `[0,n-1]`. ### Contract considerations Repeated letters remain separate source occurrences even when their characters are equal. ```hint Read the boundary condition Keep the source-to-destination direction of the index mapping. Repeated letters still represent separate source occurrences. ``` ### Boundary reminders Repeated letters remain separate source occurrences even when their characters are equal.

Overview: Move Each Character to Its Specified Destination Index. Character s[i] must move to output position indices[i].

Microsoft logo
Microsoft
Source window: 0–6 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Character s[i] must move to output position indices[i]. The indices array is a permutation of all valid positions. Return the resulting string.

class Solution {
public:
    string restoreString(string s, vector<int>& indices);
};

Examples

Example 1:

Input: s = "abcd", indices = [2,0,3,1]
Output: "bdac"
Explanation: b goes to0,d to1,a to2,and c to3.

Example 2:

Input: s = "xy", indices = [1,0]
Output: "yx"
Explanation: The two destination positions swap the characters.

Constraints

  • s.length equals indices.length equals n,with 1 <= n <= 100 .
  • s contains only lowercase English letters.
  • All indices are unique and lie in [0,n-1] .

Contract considerations

Repeated letters remain separate source occurrences even when their characters are equal.

Boundary reminders

Repeated letters remain separate source occurrences even when their characters are equal.