# 942.Di String Match

**942.Di String Match**

> 给定只含 "I"（增大）或 "D"（减小）的字符串 S ，令 N = S.length。 返回 \[0, 1, ..., N] 的任意排列 A 使得对于所有 i = 0, ..., N-1，都有： 如果 S\[i] == "I"，那么 A\[i] < A\[i+1] 如果 S\[i] == "D"，那么 A\[i] > A\[i+1]

```
示例 1：

输出："IDID"
输出：[0,4,1,3,2]
示例 2：

输出："III"
输出：[0,1,2,3]
示例 3：

输出："DDI"
输出：[3,2,0,1]

提示：
1 <= S.length <= 1000
S 只包含字符 "I" 或 "D"。
```

方法： D表示大于，初始时候从0到N，全为I，当中间出现D的时候，可以将最大的n个数放在n个D处，满足条件。第一个D出现的位置是最大值，第二个是次大值...以此类推，代码如下：

```
class Solution {
public:
    vector<int> diStringMatch(string S) {
        vector<int> key;
        int max=S.length(),min=0;
        for(int i=0;i<S.length()+1;i++)
        {
            if(S[i]=='D')
                key.push_back(max--);
            else
                key.push_back(min++);
           }

        return key;
    }
};
```


---

# 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://dfine.gitbook.io/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.
