Leetcode 484 Solution

This article provides solution to leetcode question 484 (find-permutation)

https://leetcode.com/problems/find-permutation

Solution

class Solution {
public:
    vector<int> findPermutation(string s) {
        vector<int> res;
        for (int i = 0; i < s.size() + 1; i++)
            res.push_back(i + 1);

        int l = 0;
        while (l < s.size())
        {
            if (s[l] == 'I')
                l++;
            else if (s[l] == 'D')
            {
                int r = l;
                while (r < s.size() - 1 && s[r + 1] == 'D')
                    r++;

                reverse(res.begin() + l, res.begin() + r + 2);
                l = r + 1;
            }
        }

        return res;
    }
};