
- 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
Valid Permutations for DI Sequence in C++
Suppose we have a string S. This is a string of characters from the set {'D', 'I'}. (D means "decreasing" and I means "increasing")
Now consider a valid permutation is a permutation P[0], P[1], ..., P[n] of integers {0 to n}, such that for all i, it meets these rules:
If S[i] == 'D', then P[i] > P[i+1];
Otherwise when S[i] == 'I', then P[i] < P[i+1].
We have to find how many valid permutations are there? The answer may be very large, so we will return using mod 10^9 + 7.
So, if the input is like "IDD", then the output will be 3, So there will be three different permutations, these are like (0,3,2,1), (1,3,2,0), (2,3,1,0).
To solve this, we will follow these steps −
n := size of S
Define one 2D array dp of size (n + 1) x (n + 1)
for initialize j := 0, when j <= n, update (increase j by 1), do −
dp[0, j] := 1
for initialize i := 0, when i < n, update (increase i by 1), do −
if S[i] is same as 'I', then −
for initialize j := 0, curr := 0, when j < n - i, update (increase j by 1), do −
curr := (dp[i, j] + curr) mod m
dp[i + 1, j] = (dp[i + 1, j] + curr)
otherwise
for initialize j := n - i - 1, curr := 0, when j >= 0, update (decrease j by 1), do −
curr := (dp[i, j + 1] + curr) mod m
dp[i + 1, j] = (dp[i + 1, j] + curr)
return dp[n, 0]
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; const int m = 1e9 + 7; class Solution { public: int numPermsDISequence(string S) { int n = S.size(); vector<vector<int>> dp(n + 1, vector<int>(n + 1)); for (int j = 0; j <= n; j++) dp[0][j] = 1; for (int i = 0; i < n; i++) { if (S[i] == 'I') { for (int j = 0, curr = 0; j < n - i; j++) { curr = (dp[i][j] + curr) % m; dp[i + 1][j] = (dp[i + 1][j] + curr) % m; } } else { for (int j = n - i - 1, curr = 0; j >= 0; j--) { curr = (dp[i][j + 1] + curr) % m; dp[i + 1][j] = (dp[i + 1][j] + curr) % m; } } } return dp[n][0]; } }; main(){ Solution ob; cout << (ob.numPermsDISequence("IDD")); }
Input
"IDD"
Output
3
- Related Articles
- Program to construct the lexicographically largest valid sequence in Python
- Permutations II in C++
- DI String Match in Python
- Number of Valid Words for Each Puzzle in C++
- Program to check how many queries finds valid arithmetic sequence in Python
- EI and DI instructions in 8085
- Valid Parentheses in C++
- Valid Sudoku in C++
- Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree in C++
- Missing Permutations in a list in C++
- Valid Parenthesis String in C++
- Valid Palindrome III in C++
- Valid Triangle Number in C++
- Graph Valid Tree in C++
- Valid variant of Main() in C#
