Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.
Example 1:
Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3]
Output: "leetcode"
Explanation: As shown, "codeleet" becomes "leetcode" after shuffling.
Example 2:
Input: s = "abc", indices = [0,1,2]
Output: "abc"
Explanation: After shuffling, each character remains in its position.
Constraints:
s.length == indices.length == n1 <= n <= 100s consists of only lowercase English letters.0 <= indices[i] < nindices are unique.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
"codeleet" [4,5,6,7,0,2,1,3]
"abc" [0,1,2]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1528: Shuffle String
class Solution {
public String restoreString(String s, int[] indices) {
int n = s.length();
char[] ans = new char[n];
for (int i = 0; i < n; ++i) {
ans[indices[i]] = s.charAt(i);
}
return new String(ans);
}
}
// Accepted solution for LeetCode #1528: Shuffle String
func restoreString(s string, indices []int) string {
ans := make([]rune, len(s))
for i, c := range s {
ans[indices[i]] = c
}
return string(ans)
}
# Accepted solution for LeetCode #1528: Shuffle String
class Solution:
def restoreString(self, s: str, indices: List[int]) -> str:
ans = [None] * len(s)
for c, j in zip(s, indices):
ans[j] = c
return "".join(ans)
// Accepted solution for LeetCode #1528: Shuffle String
impl Solution {
pub fn restore_string(s: String, indices: Vec<i32>) -> String {
let n = s.len();
let mut ans = vec![' '; n];
let chars: Vec<char> = s.chars().collect();
for i in 0..n {
ans[indices[i] as usize] = chars[i];
}
ans.iter().collect()
}
}
// Accepted solution for LeetCode #1528: Shuffle String
function restoreString(s: string, indices: number[]): string {
const ans: string[] = [];
for (let i = 0; i < s.length; i++) {
ans[indices[i]] = s[i];
}
return ans.join('');
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.