Problem-Solving with Leetcode | Shuffle String | Easy 1

Let’s talk about a LeetCode easy problem. Today we’ll discuss the Shuffle String (LeetCode number 1528).

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 ith position moves to indices[i] in the shuffled string.

Return the shuffled string.

Shuffle String | Leetcode 1528

From the above example, you may understand the problem. But how do we step through its solution? If you can’t understand the problem, I have a little explanation for you. As input, you have given a string, S, and its index value, indices, by the corresponding character. You have to place the character according to their corresponding index.

So, how do we solve it both in Python and Golang?

The main idea to solve the problem is, to insert the character with its corresponding index and build a string and return it. It is quite an easy task.

First, initialize a list with the length of the given string. Run a for loop to iterate over the list and insert the character to its corresponding index. Then join the list and return. Yeah, you have done it. For your help, here I share my python and Golang code.

Happy Coding :)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store