Skip to content

Commit

Permalink
Update 484.Find-Permutation.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Aug 28, 2022
1 parent fd42556 commit adf2d44
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions Greedy/484.Find-Permutation/484.Find-Permutation.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
class Solution {
public:
vector<int> findPermutation(string s)
vector<int> findPermutation(string pattern)
{
s.insert(s.begin(),s[0]);
int N=s.size();
vector<int>results(N,0);
pattern = "I" + pattern;
int n = pattern.size();

int left=0;
int right=0;
int mx = 0;
vector<int>arr;

while (right<s.size())
for (int i=0; i<n; i++)
{
while (right+1<s.size() && !(s[right]=='D' && s[right+1]=='I'))
right++;
if (right==s.size()) right--;

int top=left;
while (s[top+1]=='I') top++;
for (int i=left; i<top; i++)
results[i]=i+1;
for (int i=top; i<=right; i++)
results[i]=top+1+right-i;
left=right+1;
right=right+1;
int j = i+1;
while (j<n && pattern[j]=='D')
j++;
int count = j-i;
for (int k= mx+count; k>=mx+1; k--)
arr.push_back(k);
mx = mx+count;
i = j-1;
}

return results;

return arr;
}
};

0 comments on commit adf2d44

Please sign in to comment.