|
| 1 | +// Source : https://leetcode.com/problems/shuffle-string/submissions/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2021-03-29 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * Given a string s and an integer array indices of the same length. |
| 8 | + * |
| 9 | + * The string s will be shuffled such that the character at the i^th position moves to indices[i] in |
| 10 | + * the shuffled string. |
| 11 | + * |
| 12 | + * Return the shuffled string. |
| 13 | + * |
| 14 | + * Example 1: |
| 15 | + * |
| 16 | + * Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3] |
| 17 | + * Output: "leetcode" |
| 18 | + * Explanation: As shown, "codeleet" becomes "leetcode" after shuffling. |
| 19 | + * |
| 20 | + * Example 2: |
| 21 | + * |
| 22 | + * Input: s = "abc", indices = [0,1,2] |
| 23 | + * Output: "abc" |
| 24 | + * Explanation: After shuffling, each character remains in its position. |
| 25 | + * |
| 26 | + * Example 3: |
| 27 | + * |
| 28 | + * Input: s = "aiohn", indices = [3,1,4,2,0] |
| 29 | + * Output: "nihao" |
| 30 | + * |
| 31 | + * Example 4: |
| 32 | + * |
| 33 | + * Input: s = "aaiougrt", indices = [4,0,2,6,7,3,1,5] |
| 34 | + * Output: "arigatou" |
| 35 | + * |
| 36 | + * Example 5: |
| 37 | + * |
| 38 | + * Input: s = "art", indices = [1,0,2] |
| 39 | + * Output: "rat" |
| 40 | + * |
| 41 | + * Constraints: |
| 42 | + * |
| 43 | + * s.length == indices.length == n |
| 44 | + * 1 <= n <= 100 |
| 45 | + * s contains only lower-case English letters. |
| 46 | + * 0 <= indices[i] < n |
| 47 | + * All values of indices are unique (i.e. indices is a permutation of the integers from 0 to n |
| 48 | + * - 1). |
| 49 | + ******************************************************************************************************/ |
| 50 | + |
| 51 | +class Solution { |
| 52 | +public: |
| 53 | + string restoreString(string s, vector<int>& indices) { |
| 54 | + string result(s.size(), ' '); |
| 55 | + for (int i = 0; i < indices.size(); i++) { |
| 56 | + result[indices[i]] = s[i]; |
| 57 | + } |
| 58 | + return result; |
| 59 | + } |
| 60 | + |
| 61 | +}; |
0 commit comments