File: shuffle-string/solution.py

Date: 2026-06-06

Time: 19:05

shuffle-string/solution.py

Purpose

This file solves LeetCode 1528 — Shuffle String. Given a string s and an integer array indices of the same length, it rearranges the string so that character s[i] moves to position indices[i] in the result.

Key Components

Solution.kidswithcandies — The method name is wrong; it's a copy-paste artifact from another problem (likely LeetCode 1431). The actual behavior is restoreString: it scatters characters from s into their target positions as specified by indices, then joins into a final string.

Contract:

Patterns

Scatter-write into a pre-allocated array. Rather than building the result by sorting or inserting, it allocates a list of len(s) empty strings, then directly places each character at its destination index. This is the standard O(n) approach for permutation-based rearrangement — one pass, no sorting.

Dependencies

Flow

1. Allocate result as a list of n empty strings.

2. Iterate over s with enumerate, getting each character's current index i and its target index idx = indices[i].

3. Write s[i] into result[idx].

4. Join and return.

Invariants

Error Handling

None. Invalid inputs (wrong-length indices, out-of-bounds values, None) propagate as unhandled Python exceptions. This is typical for LeetCode solutions where input validity is guaranteed by the problem constraints.

Notable Issue

The method is named kidswithcandies instead of something like restoreString or shuffle_string. This is a bug in the code generation pipeline — the wrong method name was templated in. It doesn't affect correctness (callers just use whatever name is on Solution), but it's confusing for anyone reading the code.