DEV Community

Cover image for Leet code — 1221. Split a String in Balanced Strings
Ben Pereira
Ben Pereira

Posted on • Edited on

Leet code — 1221. Split a String in Balanced Strings

It's an easy problem by Leet Code, the description being:

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.

The image on the problem helps a lot to understand what needs to be done:

Image description

Indices array values give the index that the char from the string s supposed to be, iterating the indices and using that number to build a new String is the way to go.

class Solution {
    public String restoreString(String s, int[] indices) {
        char[] restoredString = new char[s.length()];
        for(int i=0;i<s.length();i++){
            restoredString[indices[i]] = s.charAt(i);
        }
        return new String(restoredString);
    }
}
Enter fullscreen mode Exit fullscreen mode

If there is anything thing else to discuss feel free to drop a comment, until next post! :)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more