---
# System prepended metadata

title: 0942. DI String Match
tags: [Leetcode, DI Sequence, Easy, Greedy]

---

# 0942. DI String Match
###### tags: `Leetcode` `Easy` `DI Sequence` `Greedy`
Link: https://leetcode.com/problems/di-string-match/description/
## 思路 $O(N)$ $O(N)$
greedy的思想:
遇到```I```当前数字能填多小填多小
遇到```D```当前数字能填多大填多大
## Code
```java=
class Solution {
    public int[] diStringMatch(String s) {
        int n = s.length();
        int[] arr = new int[n+1];
        int left = 0, right = n;
        for(int i=0; i<s.length(); i++){
            if(s.charAt(i)=='D') arr[i] = right--;
            else arr[i] = left++;
        }
        arr[n] = left;
        return arr;
    }
}
```