-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindPermutation.cpp
More file actions
51 lines (37 loc) · 892 Bytes
/
FindPermutation.cpp
File metadata and controls
51 lines (37 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
Given a positive integer n and a string s consisting only of letters D or I, you have to find any permutation of first n positive integer that satisfy the given input string.
D means the next number is smaller, while I means the next number is greater.
Notes
Length of given string s will always equal to n - 1
Your solution should run in linear time and space.
Example :
Input 1:
n = 3
s = ID
Return: [1, 3, 2]
LINK: https://www.interviewbit.com/problems/find-permutation/
*/
vector<int> Solution::findPerm(const string s, int n)
{
vector<int> v(n);
int len = s.length();
int k=1;
for(int i=0;i<len;i++)
{
if(s[i]=='I')
{
v[i] = k;
k++;
}
}
v[n-1] = k++;
for(int i=len-1;i>=0;i--)
{
if(s[i]=='D')
{
v[i] = k;
k++;
}
}
return v;
}