|
| 1 | +--- |
| 2 | + |
| 3 | +id: shuffle-string |
| 4 | +title: Shuffle String Solution |
| 5 | +sidebar_label: 1528-Shuffle-String |
| 6 | +tags: |
| 7 | + - Array |
| 8 | + - String |
| 9 | + - LeetCode |
| 10 | + - JavaScript |
| 11 | + - TypeScript |
| 12 | + - Python |
| 13 | + - Java |
| 14 | + - C++ |
| 15 | +description: "This is a solution to the Shuffle String problem on LeetCode." |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +In this page, we will solve the Shuffle String problem using different approaches: simple iteration and an optimized version using map. We will provide the implementation of the solution in JavaScript, TypeScript, Python, Java, C++, and more. |
| 20 | + |
| 21 | +## Problem Description |
| 22 | + |
| 23 | +You are given a string `s` and an integer array `indices` of the same length. The string `s` will be shuffled such that the character at the `i`th position moves to `indices[i]` in the shuffled string. |
| 24 | + |
| 25 | +Return the shuffled string. |
| 26 | + |
| 27 | +### Examples |
| 28 | + |
| 29 | +**Example 1:** |
| 30 | + |
| 31 | +```plaintext |
| 32 | +Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3] |
| 33 | +Output: "leetcode" |
| 34 | +Explanation: As shown, "codeleet" becomes "leetcode" after shuffling. |
| 35 | +``` |
| 36 | + |
| 37 | +**Example 2:** |
| 38 | + |
| 39 | +```plaintext |
| 40 | +Input: s = "abc", indices = [0,1,2] |
| 41 | +Output: "abc" |
| 42 | +Explanation: After shuffling, each character remains in its position. |
| 43 | +``` |
| 44 | + |
| 45 | +### Constraints |
| 46 | + |
| 47 | +- `s.length == indices.length == n` |
| 48 | +- `1 <= n <= 100` |
| 49 | +- `s` consists of only lowercase English letters. |
| 50 | +- `0 <= indices[i] < n` |
| 51 | +- All values of `indices` are unique. |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +## Solution for Shuffle String Problem |
| 56 | + |
| 57 | +### Intuition and Approach |
| 58 | + |
| 59 | +The problem can be solved by creating a new array for the shuffled string, placing each character at the position specified by the `indices` array. We will demonstrate a simple iteration approach and an optimized version using map. |
| 60 | + |
| 61 | +<Tabs> |
| 62 | + <tabItem value="Simple Iteration" label="Simple Iteration"> |
| 63 | + |
| 64 | +### Approach 1: Simple Iteration |
| 65 | + |
| 66 | +We iterate through the string and place each character at the position specified by the `indices` array. |
| 67 | + |
| 68 | +#### Implementation |
| 69 | + |
| 70 | +```jsx live |
| 71 | +function shuffleString() { |
| 72 | + const s = "codeleet"; |
| 73 | + const indices = [4, 5, 6, 7, 0, 2, 1, 3]; |
| 74 | + |
| 75 | + const restoreString = function(s, indices) { |
| 76 | + let shuffled = new Array(s.length); |
| 77 | + for (let i = 0; i < s.length; i++) { |
| 78 | + shuffled[indices[i]] = s[i]; |
| 79 | + } |
| 80 | + return shuffled.join(''); |
| 81 | + }; |
| 82 | + |
| 83 | + const result = restoreString(s, indices); |
| 84 | + return ( |
| 85 | + <div> |
| 86 | + <p> |
| 87 | + <b>Input:</b> s = "{s}", indices = {JSON.stringify(indices)} |
| 88 | + </p> |
| 89 | + <p> |
| 90 | + <b>Output:</b> {result} |
| 91 | + </p> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +#### Code in Different Languages |
| 98 | + |
| 99 | +<Tabs> |
| 100 | + <TabItem value="JavaScript" label="JavaScript" default> |
| 101 | + <SolutionAuthor name="@manishh12"/> |
| 102 | + ```javascript |
| 103 | + function restoreString(s, indices) { |
| 104 | + let shuffled = new Array(s.length); |
| 105 | + for (let i = 0; i < s.length; i++) { |
| 106 | + shuffled[indices[i]] = s[i]; |
| 107 | + } |
| 108 | + return shuffled.join(''); |
| 109 | + } |
| 110 | + ``` |
| 111 | + |
| 112 | + </TabItem> |
| 113 | + <TabItem value="TypeScript" label="TypeScript"> |
| 114 | + <SolutionAuthor name="@manishh12"/> |
| 115 | + ```typescript |
| 116 | + function restoreString(s: string, indices: number[]): string { |
| 117 | + let shuffled: string[] = new Array(s.length); |
| 118 | + for (let i = 0; i < s.length; i++) { |
| 119 | + shuffled[indices[i]] = s[i]; |
| 120 | + } |
| 121 | + return shuffled.join(''); |
| 122 | + } |
| 123 | + ``` |
| 124 | + |
| 125 | + </TabItem> |
| 126 | + <TabItem value="Python" label="Python"> |
| 127 | + <SolutionAuthor name="@manishh12"/> |
| 128 | + ```python |
| 129 | + class Solution: |
| 130 | + def restoreString(self, s: str, indices: List[int]) -> str: |
| 131 | + shuffled = [''] * len(s) |
| 132 | + for i, index in enumerate(indices): |
| 133 | + shuffled[index] = s[i] |
| 134 | + return ''.join(shuffled) |
| 135 | + ``` |
| 136 | + |
| 137 | + </TabItem> |
| 138 | + <TabItem value="Java" label="Java"> |
| 139 | + <SolutionAuthor name="@manishh12"/> |
| 140 | + ```java |
| 141 | + class Solution { |
| 142 | + public String restoreString(String s, int[] indices) { |
| 143 | + char[] shuffled = new char[s.length()]; |
| 144 | + for (int i = 0; i < s.length(); i++) { |
| 145 | + shuffled[indices[i]] = s.charAt(i); |
| 146 | + } |
| 147 | + return new String(shuffled); |
| 148 | + } |
| 149 | + } |
| 150 | + ``` |
| 151 | + |
| 152 | + </TabItem> |
| 153 | + <TabItem value="C++" label="C++"> |
| 154 | + <SolutionAuthor name="@manishh12"/> |
| 155 | + ```cpp |
| 156 | + class Solution { |
| 157 | + public: |
| 158 | + string restoreString(string s, vector<int>& indices) { |
| 159 | + string shuffled(s.length(), ' '); |
| 160 | + for (int i = 0; i < s.length(); i++) { |
| 161 | + shuffled[indices[i]] = s[i]; |
| 162 | + } |
| 163 | + return shuffled; |
| 164 | + } |
| 165 | + }; |
| 166 | + ``` |
| 167 | + |
| 168 | + </TabItem> |
| 169 | +</Tabs> |
| 170 | + |
| 171 | +#### Complexity Analysis |
| 172 | + |
| 173 | +- Time Complexity: $$O(n)$$, where `n` is the length of the string. |
| 174 | +- Space Complexity: $$O(n)$$, as we are using an additional array to store the shuffled string. |
| 175 | + |
| 176 | +</tabItem> |
| 177 | + |
| 178 | +<tabItem value="Using Map" label="Using Map"> |
| 179 | + |
| 180 | +### Approach 2: Using Map |
| 181 | + |
| 182 | +We can use a map to store the characters with their respective indices and then reconstruct the shuffled string. |
| 183 | + |
| 184 | +#### Implementation |
| 185 | + |
| 186 | +```jsx live |
| 187 | +function shuffleString() { |
| 188 | + const s = "codeleet"; |
| 189 | + const indices = [4, 5, 6, 7, 0, 2, 1, 3]; |
| 190 | +
|
| 191 | + const restoreString = function(s, indices) { |
| 192 | + let map = new Map(); |
| 193 | + for (let i = 0; i < s.length; i++) { |
| 194 | + map.set(indices[i], s[i]); |
| 195 | + } |
| 196 | + let shuffled = ''; |
| 197 | + for (let i = 0; i < s.length; i++) { |
| 198 | + shuffled += map.get(i); |
| 199 | + } |
| 200 | + return shuffled; |
| 201 | + }; |
| 202 | +
|
| 203 | + const result = restoreString(s, indices); |
| 204 | + return ( |
| 205 | + <div> |
| 206 | + <p> |
| 207 | + <b>Input:</b> s = "{s}", indices = {JSON.stringify(indices)} |
| 208 | + </p> |
| 209 | + <p> |
| 210 | + <b>Output:</b> {result} |
| 211 | + </p> |
| 212 | + </div> |
| 213 | + ); |
| 214 | +} |
| 215 | +``` |
| 216 | + |
| 217 | +#### Code in Different Languages |
| 218 | + |
| 219 | +<Tabs> |
| 220 | + <TabItem value="JavaScript" label="JavaScript" default> |
| 221 | + <SolutionAuthor name="@manishh12"/> |
| 222 | + ```javascript |
| 223 | + function restoreString(s, indices) { |
| 224 | + let map = new Map(); |
| 225 | + for (let i = 0; i < s.length; i++) { |
| 226 | + map.set(indices[i], s[i]); |
| 227 | + } |
| 228 | + let shuffled = ''; |
| 229 | + for (let i = 0; i < s.length; i++) { |
| 230 | + shuffled += map.get(i); |
| 231 | + } |
| 232 | + return shuffled; |
| 233 | + } |
| 234 | + ``` |
| 235 | + |
| 236 | + </TabItem> |
| 237 | + <TabItem value="TypeScript" label="TypeScript"> |
| 238 | + <SolutionAuthor name="@manishh12"/> |
| 239 | + ```typescript |
| 240 | + function restoreString(s: string, indices: number[]): string { |
| 241 | + let map: Map<number, string> = new Map(); |
| 242 | + for (let i = 0; i < s.length; i++) { |
| 243 | + map.set(indices[i], s[i]); |
| 244 | + } |
| 245 | + let shuffled: string = ''; |
| 246 | + for (let i = 0; i < s.length; i++) { |
| 247 | + shuffled += map.get(i); |
| 248 | + } |
| 249 | + return shuffled; |
| 250 | + } |
| 251 | + ``` |
| 252 | + |
| 253 | + </TabItem> |
| 254 | + <TabItem value="Python" label="Python"> |
| 255 | + <SolutionAuthor name="@manishh12"/> |
| 256 | + ```python |
| 257 | + class Solution: |
| 258 | + def restoreString(self, s: str, indices: List[int]) -> str: |
| 259 | + map = {indices[i]: s[i] for i in range(len(s))} |
| 260 | + shuffled = ''.join(map[i] for i in range(len(s))) |
| 261 | + return shuffled |
| 262 | + ``` |
| 263 | + |
| 264 | + </TabItem> |
| 265 | + <TabItem value="Java" label="Java"> |
| 266 | + <SolutionAuthor name="@manishh12"/> |
| 267 | + ```java |
| 268 | + import java.util.HashMap; |
| 269 | + import java.util.Map; |
| 270 | +
|
| 271 | + class Solution { |
| 272 | + public String restoreString(String s, int[] indices) { |
| 273 | + Map<Integer, Character> map = new HashMap<>(); |
| 274 | + for (int i = 0; i < s.length(); i++) { |
| 275 | + map.put(indices[i], s.charAt(i)); |
| 276 | + } |
| 277 | + StringBuilder shuffled = new StringBuilder(); |
| 278 | + for (int i = 0; i < s.length(); i++) { |
| 279 | + shuffled.append(map.get(i)); |
| 280 | + } |
| 281 | + return shuffled.toString(); |
| 282 | + } |
| 283 | + } |
| 284 | + ``` |
| 285 | + |
| 286 | + </TabItem> |
| 287 | + <TabItem value="C++" label="C++"> |
| 288 | + <SolutionAuthor name="@manishh12"/> |
| 289 | + ```cpp |
| 290 | + #include <unordered_map> |
| 291 | + #include <string> |
| 292 | +
|
| 293 | + class Solution { |
| 294 | + public: |
| 295 | + string restoreString(string s, vector<int>& indices) { |
| 296 | + unordered_map<int, char> map; |
| 297 | + for (int i = 0; i < s.length(); i++) { |
| 298 | + map[indices[i]] = s[i]; |
| 299 | + } |
| 300 | + string shuffled(s.length(), ' '); |
| 301 | + for (int i = 0; i < s.length(); i++) { |
| 302 | + shuffled[i] = map[i]; |
| 303 | + } |
| 304 | + return shuffled; |
| 305 | + } |
| 306 | + }; |
| 307 | + ``` |
| 308 | + |
| 309 | + </TabItem> |
| 310 | +</Tabs> |
| 311 | + |
| 312 | +#### Complexity Analysis |
| 313 | + |
| 314 | +- Time Complexity: $$O(n)$$, where `n` is the length of the string. |
| 315 | +- Space Complexity: $$O(n)$$, as we are using a map to store the characters. |
| 316 | + |
| 317 | +</tabItem> |
| 318 | +</Tabs> |
| 319 | + |
| 320 | +:::tip Note |
| 321 | + |
| 322 | +By using both simple iteration and map-based approaches, we can efficiently solve the Shuffle String problem. The choice between the two approaches depends on the specific requirements and constraints of the problem. |
| 323 | + |
| 324 | +::: |
| 325 | + |
| 326 | +## References |
| 327 | + |
| 328 | +- **LeetCode Problem:** [Shuffle String](https://leetcode.com/problems/shuffle-string/) |
| 329 | +- **Solution Link:** [Shuffle String Solution on LeetCode](https://leetcode.com/problems/shuffle-string/solution/) |
| 330 | +- **Authors LeetCode Profile:** [Manish Kumar Gupta](https://leetcode.com/_manishh12/) |
| 331 | + |
| 332 | + |
0 commit comments