Leet code — 1221. Split a String in Balanced Strings

Ben Pereira - Aug 20 '23 - - Dev Community

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! :)

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .