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.
A permutation perm of n + 1 integers of all the integers in the range [0, n] can be represented as a string s of length n where:
s[i] == 'I' if perm[i] < perm[i + 1], ands[i] == 'D' if perm[i] > perm[i + 1].Given a string s, reconstruct the permutation perm and return it. If there are multiple valid permutations perm, return any of them.
Example 1:
Input: s = "IDID" Output: [0,4,1,3,2]
Example 2:
Input: s = "III" Output: [0,1,2,3]
Example 3:
Input: s = "DDI" Output: [3,2,0,1]
Constraints:
1 <= s.length <= 105s[i] is either 'I' or 'D'.Problem summary: A permutation perm of n + 1 integers of all the integers in the range [0, n] can be represented as a string s of length n where: s[i] == 'I' if perm[i] < perm[i + 1], and s[i] == 'D' if perm[i] > perm[i + 1]. Given a string s, reconstruct the permutation perm and return it. If there are multiple valid permutations perm, return any of them.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Greedy
"IDID"
"III"
"DDI"
construct-smallest-number-from-di-string)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #942: DI String Match
class Solution {
public int[] diStringMatch(String s) {
int n = s.length();
int low = 0, high = n;
int[] ans = new int[n + 1];
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'I') {
ans[i] = low++;
} else {
ans[i] = high--;
}
}
ans[n] = low;
return ans;
}
}
// Accepted solution for LeetCode #942: DI String Match
func diStringMatch(s string) (ans []int) {
low, high := 0, len(s)
for _, c := range s {
if c == 'I' {
ans = append(ans, low)
low++
} else {
ans = append(ans, high)
high--
}
}
ans = append(ans, low)
return
}
# Accepted solution for LeetCode #942: DI String Match
class Solution:
def diStringMatch(self, s: str) -> List[int]:
low, high = 0, len(s)
ans = []
for c in s:
if c == "I":
ans.append(low)
low += 1
else:
ans.append(high)
high -= 1
ans.append(low)
return ans
// Accepted solution for LeetCode #942: DI String Match
impl Solution {
pub fn di_string_match(s: String) -> Vec<i32> {
let mut low = 0;
let mut high = s.len() as i32;
let mut ans = Vec::with_capacity(s.len() + 1);
for c in s.chars() {
if c == 'I' {
ans.push(low);
low += 1;
} else {
ans.push(high);
high -= 1;
}
}
ans.push(low);
ans
}
}
// Accepted solution for LeetCode #942: DI String Match
function diStringMatch(s: string): number[] {
const ans: number[] = [];
let [low, high] = [0, s.length];
for (const c of s) {
if (c === 'I') {
ans.push(low++);
} else {
ans.push(high--);
}
}
ans.push(low);
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
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.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.