Leetcode Problem 942. DI String Match

942. DI String Match

Leetcode Solutions

Greedy Approach with Two Pointers

  1. Initialize low to 0 and high to the length of the string s.
  2. Create an array perm of size n + 1 to hold the permutation.
  3. Iterate through the string s using an index i:
    • If s[i] is 'I', set perm[i] to low and increment low.
    • If s[i] is 'D', set perm[i] to high and decrement high.
  4. After the loop, set perm[n] to the remaining value of low (which is equal to high at this point).
  5. Return the permutation array perm.
UML Thumbnail

Backtracking Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...