Brilliant idea!

Here's my C++ version using STL methods.

class Solution { public: vector<int> findPermutation(string s) { int n = (int)s.length() + 1; vector<int> nums(n); for (int i = 1; i <= n; ++i) nums[i - 1] = i; for (int l = 0, h = 0; l < n - 1; l = h + 1) { for (h = l; h < n - 1 && s[h] == 'D'; ++h); reverse(nums.begin() + l, nums.begin() + h + 1); } return nums; } };