# 10. Number following a pattern

The problem can be found at the following link: [Question Link](https://www.geeksforgeeks.org/problems/number-following-a-pattern3126/1)

![](https://badgen.net/badge/Level/Medium/yellow)

## My Approach

To generate the minimum number following a given pattern,

* I iterate through the input string `S` and create a vector of pairs to store consecutive characters and their counts.
* Then, I construct the output string based on this information.
* The key idea is to increment or decrement the numbers in the output string based on whether the corresponding character in the input is `I` or `D`.
  * After observing some pattern I found that when decrementing, we need to include `count + 1` in our output, and when incrementing, we should use `count - 1` characters in the output.

## Time and Auxiliary Space Complexity

* **Time Complexity**: `O(N)`, where N is the length of the input string.
* **Auxiliary Space Complexity**: `O(N)`, for storing the pairs representing consecutive characters and their counts.

## Code (C++)

```cpp
class Solution{   
public:
    string printMinNumberForPattern(string S){
        vector<pair<char, int>> col;
        char curr = S[0];
        int c = (S[0] == 'I');
        
        for (auto i: S){
            if (i == curr)
                ++c;
            else{
                col.push_back({curr, c});
                c = 1;
                curr = i;
            }
        }               
        if (S.back() == 'I')
            ++c;
        col.push_back({curr, c});
        
        string out;
        c = 1;
        for (auto i: col){
            char op = i.first;
            int cnt = i.second + (op == 'D'? 1: -1);
            string temp;
            while (cnt--){
                temp += (char)('0' + c++);
            }
            if (op == 'D')
                reverse(temp.begin(), temp.end());
            out += temp;
        }
        return out;
    }
};
```

## Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://github.com/getlost01/gfg-potd/discussions). We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a ⭐ star to the [getlost01/gfg-potd](https://github.com/getlost01/gfg-potd) repository.


---

# 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://gl01.gitbook.io/gfg-editorials/2023/11-2023-nov-9/10-number-following-a-pattern.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.
