# 942. DI String Match

## 1.問題

* 給予一個只有'I'或'D'的字串, 回傳任意list:
  * 如果S\[i]為'I', 則A\[i] < A\[i + 1]
  * 如果S\[i]為'D', 則A\[i] > A\[i + 1]

![](/files/-LSKDfO88JFeIBDc5TnC)

## 2.想法

* 當I時, 插入前面的數字; 當D時, 插入後面的數字

## 3.程式碼

```
class Solution {
public:
    vector<int> diStringMatch(string S) {
        vector<int> res;
        int n = S.length(), f = 0, b = n;
        for (int i = 0; i <= n; i++) {
            if (S[i] == 'I') {
                res.push_back(f);
                f++;
            } else {
                res.push_back(b);
                b--;
            }
        }
        
        return res;
    }
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jenhsuan.gitbook.io/algorithm/leetcode/942.-di-string-match.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
