
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find Permutation in C++
Suppose we have a secret signature consisting of character 'D' and 'I'. 'D' denotes the decreasing relationship between two numbers, 'I' denotes increasing relationship between two numbers. And the secret signature was constructed by a special integer array, which contains uniquely all the different number from 1 to n.
For example, the secret signature "DI" can be constructed from an array like [2,1,3] or [3,1,2], but not be constructed using array like [3,2,4] or [2,1,3,4], which are both illegal constructing special string that can't represent the "DI" secret signature.
Now we have to find the lexicographically smallest permutation of [1, 2, ... n] that could refer to the given secret signature in the input.
So, if the input is like "DI", then the output will be [2,1,3], As we know [3,1,2] can also construct the secret signature "DI", but since we want to find the one with the smallest lexicographical permutation, we need to output [2,1,3]
To solve this, we will follow these steps −
Define one stack st
cnt := 2
Define an array ret
for initialize i := 1, when i <= size of s, update (increase i by 1), do −
if s[i - 1] is same as 'D', then −
insert i into st
Otherwise
insert i at the end of ret
while (not st is empty), do −
insert top element of st at the end of ret
delete element from st
insert size of s into st
while (not st is empty), do −
insert top element of st at the end of ret
delete element from st
return ret
Example
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; void print_vector(vector<auto< v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << v[i] << ", "; } cout << "]"<<endl; } class Solution { public: vector<int< findPermutation(string s) { stack <int< st; int cnt = 2; vector <int< ret; for(int i = 1; i <= s.size(); i++){ if(s[i - 1] == 'D'){ st.push(i); } else{ ret.push_back(i); while(!st.empty()){ ret.push_back(st.top()); st.pop(); } } } st.push(s.size() + 1); while(!st.empty()){ ret.push_back(st.top()); st.pop(); } return ret; } }; main(){ Solution ob; print_vector(ob.findPermutation("DIID")); }
Input
"DIID"
Output
[2, 1, 3, 5, 4, ]
- Related Articles
- Find smallest permutation of given number in C++
- Program to find decode XORed permutation in Python
- Find n-th lexicographically permutation of a strings in Python
- Find n-th lexicographically permutation of a strings in C++
- Find the maximum value permutation of a graph in C++
- C++ program to find permutation with n peaks
- C++ Program to find permutation from merged permutations
- Next Permutation in Python
- Permutation Sequence in C++
- Program to find maximum sum obtained of any permutation in Python
- Permutation in String in C++
- Permutation and Combination in Java
- Lexicographically next permutation in C++
- Permutation and Combination in Python?
- Letter Case Permutation in C++
