Minimum cost to generate any permutation of the given string
Given string str of size N consisting of the first N alphabets and a matrix mat[] of size N*N where mat[i][j] represents the cost of placing ith character of the alphabet before the jth character in the string. The task is to find the minimum cost to generate any permutation of the given string.
Examples:
Input:str = “abcde”, mat[][]= {{0, 5, 1, 5, 3}, {4, 0, 9, 4, 2}, {7, 9, 0, 10, 7}, {1, 2, 8, 0, 2}, {3, 9, 7, 7, 0}}
Output: 8
Explanation:
Permutation ‘dbeac’ can be generated at a minimum cost of 8.
Cost of placing d = 0 (since there is no prev character)
Cost of placing b after d = mat[4][2] = 2
Cost of placing e after b = mat[2][5] = 2
Cost of placing a after e = mat[5][1] = 3
Cost of placing c after a = mat[1][3] = 1
Total cost = 2 + 2 + 3 + 1 = 8
Input: str = “abcde”, mat[][] = {{0, 9, 4, 8, 10}, {7, 0, 9, 5, 5}, {2, 8, 0, 4, 1}, {4, 10, 5, 0, 5}, {4, 3, 4, 7, 0 }}
Output: 13
Naive Approach: The naive idea is to generate all possible permutations of the given string and find the cost of every permutation. Then print the minimum cost among all possible costs.
Time Complexity: O(N*N!)
Auxiliary Space: O(N!)
Efficient Approach: To optimize the above approach the idea is to use Dynamic Programming with Bit Masking. Observe that all the characters are distinct and there are only 26 alphabets possible. So use a mask to store the presence of each character. Below are the steps:
- Iterate through all characters of the string and place each character at each position if it is available i.e., the bit is set in the mask.
- Then, place the character in the current position and calculate the cost of placing the character.
- Move to the next position by flipping the bit of the current character.
- At each iteration, the mask represents the number of available characters for the current position.
- After completing the above steps, print the minimum cost among all the costs calculated.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool check( int mask, int i)
{
int c = (mask & (1 << i));
return c != 0;
}
int solve(vector<vector< int >> a, string s,
int n, int prev, int mask,
vector<vector< int >> dp)
{
if (mask == 0)
return 0;
if (dp[mask][prev + 1] != -1)
return dp[mask][prev + 1];
int ans = 10000;
for ( int i = 0; i < s.length(); i++)
{
int id = s[i] - 'a' ;
if (check(mask, id))
{
if (prev == -1)
{
ans = min(ans, solve(a, s, n, id,
mask ^ (1 << id), dp));
}
else
{
ans = min(ans, a[prev][id] +
solve(a, s, n, id,
mask ^ (1 << id), dp));
}
}
}
dp[mask][prev + 1] = ans;
return ans;
}
void generatePermutation( int mask, int n,
vector<vector< int >> a,
string s)
{
vector<vector< int >> dp((1 << n) + 5 ,
vector< int > (n + 5, -1));
for ( int i = 0; i < s.length(); i++)
{
int id = s[i] - 'a' ;
mask |= (1 << id);
}
cout << solve(a, s, n, -1, mask, dp)
<< endl;
}
int main()
{
int N = 5;
string str = "abcde" ;
vector<vector< int >> mat = { { 0, 5, 1, 5, 3 },
{ 4, 0, 9, 4, 2 },
{ 7, 9, 0, 10, 7 },
{ 1, 2, 8, 0, 2 },
{ 3, 9, 7, 7, 0 } };
generatePermutation(0, N, mat, str);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int solve(
int a[][], String s, int n,
int prev, int mask, int [][] dp)
{
if (mask == 0 )
return 0 ;
if (dp[mask][prev + 1 ] != - 1 )
return dp[mask][prev + 1 ];
int ans = 10000 ;
for ( int i = 0 ; i < s.length(); i++) {
int id = s.charAt(i) - 'a' ;
if (check(mask, id)) {
if (prev == - 1 ) {
ans
= Math.min(ans,
solve(
a, s,
n, id,
mask ^ ( 1 << id),
dp));
}
else {
ans = Math.min(
ans,
a[prev][id]
+ solve(
a, s,
n, id,
mask ^ ( 1 << id),
dp));
}
}
}
dp[mask][prev + 1 ] = ans;
return ans;
}
public static boolean
check( int mask, int i)
{
int c = (mask & ( 1 << i));
return c != 0 ;
}
static void generatePermutation(
int mask, int n, int a[][],
String s)
{
int dp[][] = new int [( 1 << n) + 5 ][n + 5 ];
for ( int i[] : dp)
Arrays.fill(i, - 1 );
for ( int i = 0 ;
i < s.length(); i++) {
int id = s.charAt(i) - 'a' ;
mask |= ( 1 << id);
}
System.out.println(solve(
a, s, n, - 1 , mask, dp));
}
public static void main(String args[])
{
int N = 5 ;
String str = "abcde" ;
int mat[][] = { { 0 , 5 , 1 , 5 , 3 },
{ 4 , 0 , 9 , 4 , 2 },
{ 7 , 9 , 0 , 10 , 7 },
{ 1 , 2 , 8 , 0 , 2 },
{ 3 , 9 , 7 , 7 , 0 } };
generatePermutation( 0 , N, mat, str);
}
}
|
Python3
def solve(a, s, n, prev,
mask, dp):
if (mask = = 0 ):
return 0 ;
if (dp[mask][prev + 1 ] ! = - 1 ):
return dp[mask][prev + 1 ];
ans = 10000 ;
for i in range ( len (s)):
id = ord (s[i]) - ord ( 'a' );
if (check(mask, id )):
if (prev = = - 1 ):
ans = min (ans,
solve(a, s,
n, id ,
mask ^ ( 1 <<
id ), dp));
else :
ans = min (ans, a[prev][ id ] +
solve(a, s, n,
id , mask ^
( 1 << id ), dp));
dp[mask][prev + 1 ] = ans;
return ans;
def check(mask, i):
c = (mask & ( 1 << i));
return c ! = 0 ;
def generatePermutation(mask, n,
a, s):
dp = [[ - 1 for i in range (n + 5 )]
for j in range (( 1 << n) + 5 )]
for i in range ( len (s)):
id = ord (s[i]) - ord ( 'a' );
mask | = ( 1 << id );
print (solve(a, s, n,
- 1 , mask, dp));
if __name__ = = '__main__' :
N = 5 ;
str = "abcde" ;
mat = [[ 0 , 5 , 1 , 5 , 3 ],
[ 4 , 0 , 9 , 4 , 2 ],
[ 7 , 9 , 0 , 10 , 7 ],
[ 1 , 2 , 8 , 0 , 2 ],
[ 3 , 9 , 7 , 7 , 0 ]];
generatePermutation( 0 , N,
mat, str );
|
C#
using System;
class GFG{
public static int solve( int [,]a, String s, int n,
int prev, int mask, int [,] dp)
{
if (mask == 0)
return 0;
if (dp[mask,prev + 1] != -1)
return dp[mask, prev + 1];
int ans = 10000;
for ( int i = 0;
i < s.Length; i++)
{
int id = s[i] - 'a' ;
if (check(mask, id))
{
if (prev == -1)
{
ans = Math.Min(ans,
solve(a, s, n, id,
mask ^ (1 << id), dp));
}
else
{
ans = Math.Min(ans, a[prev,id] +
solve(a, s, n, id,
mask ^ (1 << id), dp));
}
}
}
dp[mask, prev + 1] = ans;
return ans;
}
public static bool check( int mask, int i)
{
int c = (mask & (1 << i));
return c != 0;
}
static void generatePermutation( int mask, int n,
int [,]a, String s)
{
int [,]dp = new int [(1 << n) + 5,
n + 5];
for ( int i = 0;
i < (1 << n) + 5; i++)
for ( int j = 0;
j < n + 5; j++)
dp[i, j] = -1;
for ( int i = 0;
i < s.Length; i++)
{
int id = s[i] - 'a' ;
mask |= (1 << id);
}
Console.WriteLine(solve(a, s, n,
-1, mask, dp));
}
public static void Main(String []args)
{
int N = 5;
String str = "abcde" ;
int [,]mat = {{0, 5, 1, 5, 3},
{4, 0, 9, 4, 2},
{7, 9, 0, 10, 7},
{1, 2, 8, 0, 2},
{3, 9, 7, 7, 0}};
generatePermutation(0, N, mat, str);
}
}
|
Javascript
<script>
function check(mask, i)
{
var c = (mask & (1 << i));
return c != 0;
}
function solve(a, s, n, prev, mask, dp)
{
if (mask == 0)
return 0;
if (dp[mask][prev + 1] != -1)
return dp[mask][prev + 1];
var ans = 10000;
for ( var i = 0; i < s.length; i++)
{
var id = s[i].charCodeAt(0) - 'a' .charCodeAt(0);
if (check(mask, id))
{
if (prev == -1)
{
ans = Math.min(ans, solve(a, s, n, id,
mask ^ (1 << id), dp));
}
else
{
ans = Math.min(ans, a[prev][id] +
solve(a, s, n, id,
mask ^ (1 << id), dp));
}
}
}
dp[mask][prev + 1] = ans;
return ans;
}
function generatePermutation(mask, n, a, s)
{
var dp = Array.from(Array((1 << n) + 5), ()=> Array(n + 5).fill(-1));
for ( var i = 0; i < s.length; i++)
{
var id = s[i].charCodeAt(0) - 'a' .charCodeAt(0);
mask |= (1 << id);
}
document.write( solve(a, s, n, -1, mask, dp) + "<br>" );
}
var N = 5;
var str = "abcde" ;
var mat = [ [ 0, 5, 1, 5, 3 ],
[ 4, 0, 9, 4, 2 ],
[ 7, 9, 0, 10, 7 ],
[ 1, 2, 8, 0, 2 ],
[ 3, 9, 7, 7, 0 ] ];
generatePermutation(0, N, mat, str);
</script>
|
Time Complexity: O(N*2N)
Auxiliary Space: O(N*2N)
Using Brute Force with Recursion in python:
Approach:
The idea is to generate all possible permutations of the given string and calculate the cost for each permutation. Finally, return the minimum cost among all the permutations.
The get_min_cost function takes two inputs, a string str and a 2D matrix mat that represents the cost of swapping each pair of characters. The function returns the minimum cost of swapping any pair of characters in the input string such that the resulting string is lexicographically sorted.
The function first generates all permutations of the input string using a recursive function permute, which takes the current permutation string s, left and right indices of the current permutation l and r respectively. The function generates permutations by swapping each character with every other character in the string, recursively calling itself for each swap until the left and right indices are the same, indicating that the string is fully permuted.
For each permutation, the function calculates the cost of swapping pairs of characters using the input matrix mat and returns the minimum cost of all permutations. The cost is calculated by iterating through each character in the permutation, and adding the cost of swapping it with the previous character. The cost of swapping two characters is obtained by indexing the matrix using the ASCII value of the characters.
C++
#include <bits/stdc++.h>
using namespace std;
void permute(string s, int l, int r, vector<string>& perms) {
if (l == r) {
perms.push_back(s);
} else {
for ( int i = l; i <= r; i++) {
swap(s[l], s[i]);
permute(s, l + 1, r, perms);
swap(s[l], s[i]);
}
}
}
int get_min_cost(string str, vector<vector< int >>& mat) {
int n = str.length();
vector<string> perms;
permute(str, 0, n - 1, perms);
int min_cost = INT_MAX;
for ( const string& perm : perms) {
int cost = 0;
for ( int i = 1; i < n; i++) {
int row = perm[i - 1] - 'a' ;
int col = perm[i] - 'a' ;
cost += mat[row][col];
}
if (cost < min_cost) {
min_cost = cost;
}
}
return min_cost;
}
int main() {
string str1 = "abcde" ;
vector<vector< int >> mat1 = {{0, 5, 1, 5, 3},
{4, 0, 9, 4, 2},
{7, 9, 0, 10, 7},
{1, 2, 8, 0, 2},
{3, 9, 7, 7, 0}};
int result = get_min_cost(str1, mat1);
cout << "Output: " << result << endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class StringPermutationMinCost {
static void permute(String s, int l, int r, List<String> perms) {
if (l == r) {
perms.add(s);
} else {
for ( int i = l; i <= r; i++) {
s = swap(s, l, i);
permute(s, l + 1 , r, perms);
s = swap(s, l, i);
}
}
}
static String swap(String str, int i, int j) {
char [] charArray = str.toCharArray();
char temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
return new String(charArray);
}
static int getMinCost(String str, int [][] mat) {
int n = str.length();
List<String> perms = new ArrayList<>();
permute(str, 0 , n - 1 , perms);
int minCost = Integer.MAX_VALUE;
for (String perm : perms) {
int cost = 0 ;
for ( int i = 1 ; i < n; i++) {
int row = perm.charAt(i - 1 ) - 'a' ;
int col = perm.charAt(i) - 'a' ;
cost += mat[row][col];
}
if (cost < minCost) {
minCost = cost;
}
}
return minCost;
}
public static void main(String[] args) {
String str1 = "abcde" ;
int [][] mat1 = {{ 0 , 5 , 1 , 5 , 3 },
{ 4 , 0 , 9 , 4 , 2 },
{ 7 , 9 , 0 , 10 , 7 },
{ 1 , 2 , 8 , 0 , 2 },
{ 3 , 9 , 7 , 7 , 0 }};
int result = getMinCost(str1, mat1);
System.out.println(result);
}
}
|
Python3
def get_min_cost( str , mat):
n = len ( str )
perms = []
def permute(s, l, r):
if l = = r:
perms.append(s.copy())
else :
for i in range (l, r + 1 ):
s[l], s[i] = s[i], s[l]
permute(s, l + 1 , r)
s[l], s[i] = s[i], s[l]
permute( list ( str ), 0 , n - 1 )
min_cost = float ( 'inf' )
for perm in perms:
cost = 0
for i in range ( 1 , n):
cost + = mat[ ord (perm[i - 1 ]) - 97 ][ ord (perm[i]) - 97 ]
if cost < min_cost:
min_cost = cost
return min_cost
str1 = "abcde"
mat1 = [[ 0 , 5 , 1 , 5 , 3 ], [ 4 , 0 , 9 , 4 , 2 ], [ 7 , 9 , 0 , 10 , 7 ], [ 1 , 2 , 8 , 0 , 2 ], [ 3 , 9 , 7 , 7 , 0 ]]
print (get_min_cost(str1, mat1))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void Permute( string s, int l, int r,
List< string > perms)
{
if (l == r) {
perms.Add(s);
}
else {
for ( int i = l; i <= r; i++) {
s = SwapChars(s, l, i);
Permute(s, l + 1, r, perms);
s = SwapChars(
s, l,
i);
}
}
}
static string SwapChars( string s, int i, int j)
{
char [] charArray = s.ToCharArray();
char temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
return new string (charArray);
}
static int GetMinCost( string str, int [][] mat)
{
int n = str.Length;
List< string > perms = new List< string >();
Permute(str, 0, n - 1, perms);
int minCost = int .MaxValue;
foreach ( string perm in perms)
{
int cost = 0;
for ( int i = 1; i < n; i++) {
int row = perm[i - 1] - 'a' ;
int col = perm[i] - 'a' ;
cost += mat[row][col];
}
if (cost < minCost) {
minCost = cost;
}
}
return minCost;
}
static void Main()
{
string str1 = "abcde" ;
int [][] mat1
= new int [][] { new int [] { 0, 5, 1, 5, 3 },
new int [] { 4, 0, 9, 4, 2 },
new int [] { 7, 9, 0, 10, 7 },
new int [] { 1, 2, 8, 0, 2 },
new int [] { 3, 9, 7, 7, 0 } };
int result = GetMinCost(str1, mat1);
Console.WriteLine(result);
}
}
|
Javascript
class GFG {
static Permute(s, l, r, perms) {
if (l === r) {
perms.push(s);
} else {
for (let i = l; i <= r; i++) {
s = this .SwapChars(s, l, i);
this .Permute(s, l + 1, r, perms);
s = this .SwapChars(s, l, i);
}
}
}
static SwapChars(s, i, j) {
const charArray = s.split( '' );
const temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
return charArray.join( '' );
}
static GetMinCost(str, mat) {
const n = str.length;
const perms = [];
this .Permute(str, 0, n - 1, perms);
let minCost = Number.MAX_SAFE_INTEGER;
perms.forEach(perm => {
let cost = 0;
for (let i = 1; i < n; i++) {
const row = perm.charCodeAt(i - 1) - 'a' .charCodeAt(0);
const col = perm.charCodeAt(i) - 'a' .charCodeAt(0);
cost += mat[row][col];
}
if (cost < minCost) {
minCost = cost;
}
});
return minCost;
}
static Main() {
const str1 = 'abcde' ;
const mat1 = [
[0, 5, 1, 5, 3],
[4, 0, 9, 4, 2],
[7, 9, 0, 10, 7],
[1, 2, 8, 0, 2],
[3, 9, 7, 7, 0],
];
const result = this .GetMinCost(str1, mat1);
console.log(result);
}
}
GFG.Main();
|
Time Complexity: O(n!), where n is the length of the string. (as we generate all permutations)
Space Complexity: O(n), where n is the length of the string. (as we store the current permutation in memory)
Approch 3 : Dynamic Programming
The provided code solves the given problem using dynamic programming. Memorization is employed to prevent recomputations and store the status of the subproblems. The current state is sent to the main function “solve” as (mask, prev), where “mask” denotes the set of characters that are now accessible and “prev” denotes the character that was previously positioned. The string is then iterated over, and each conceivable character that could fit in the present location is checked. It repeatedly calls’solve’ with the updated state and computes the cost of inserting the current character based on the previous character and the cost matrix ‘a’ for each character that may be placed at the current position. The current state’s dp table then contains the character placement with the lowest cost out of all potential character placements.
Algorithm Steps of the given code are as follows:
1. Define a function check() to check if the current bit is set in the mask.
2. Define a recursive function solve() to find the minimum cost to generate any permutation of the given string.
3. In solve(), check for the base case where mask becomes 0, i.e., all the characters have been used.
4. In solve(), check if the current state has already been computed in the dp table, if yes, then return the precomputed value.
5. In solve(), iterate over the string and check all possible characters available for the current position.
6. In solve(), if the character can be placed at the current position, then check if there is no previous character, in which case the cost for the first character is 0.
7. In solve(), else find the cost of the current character and move to the next position.
8. In solve(), store the answer for each current state in the dp table.
9. Define a function generatePermutation() to generate any permutation of the given string with minimum cost.
10. In generatePermutation(), initialize the dp table and set all the bits of the current character id.
11. In generatePermutation(), call the solve() function with initial parameters and output the minimum cost.
12. In main(), define the input string and cost matrix and call the generatePermutation() function with the inputs.
13. End of the program.
C++
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
int check( int mask, int i) {
int c = (mask & (1 << i));
return c != 0;
}
int solve(vector<vector< int >>& a, string s, int n, int prev, int mask, vector<vector< int >>& dp) {
if (mask == 0) {
return 0;
}
if (dp[mask][prev + 1] != -1) {
return dp[mask][prev + 1];
}
int ans = 10000;
for ( int i = 0; i < s.length(); i++) {
int id = s[i] - 'a' ;
if (check(mask, id)) {
if (prev == -1) {
ans = min(ans, solve(a, s, n, id, mask ^ (1 << id), dp));
}
else {
ans = min(ans, a[prev][id] + solve(a, s, n, id, mask ^ (1 << id), dp));
}
}
}
dp[mask][prev + 1] = ans;
return ans;
}
void generate_permutation( int mask, int n, vector<vector< int >>& a, string s) {
vector<vector< int >> dp((1 << n) + 5, vector< int >(n + 5, -1));
for ( int i = 0; i < s.length(); i++) {
int id = s[i] - 'a' ;
mask |= (1 << id);
}
cout << solve(a, s, n, -1, mask, dp) << endl;
}
int main() {
int N = 5;
string str = "abcde" ;
vector<vector< int >> mat = {
{0, 5, 1, 5, 3},
{4, 0, 9, 4, 2},
{7, 9, 0, 10, 7},
{1, 2, 8, 0, 2},
{3, 9, 7, 7, 0}
};
generate_permutation(0, N, mat, str);
return 0;
}
|
Java
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class MinimumCostPermutation {
public static void main(String[] args) {
int N = 5 ;
String str = "abcde" ;
List<List<Integer>> mat = Arrays.asList(
Arrays.asList( 0 , 5 , 1 , 5 , 3 ),
Arrays.asList( 4 , 0 , 9 , 4 , 2 ),
Arrays.asList( 7 , 9 , 0 , 10 , 7 ),
Arrays.asList( 1 , 2 , 8 , 0 , 2 ),
Arrays.asList( 3 , 9 , 7 , 7 , 0 )
);
generatePermutation( 0 , N, mat, str);
}
public static int check( int mask, int i) {
int c = (mask & ( 1 << i));
return c != 0 ? 1 : 0 ;
}
public static int solve(List<List<Integer>> a, String s, int n, int prev, int mask, List<List<Integer>> dp) {
if (mask == 0 ) {
return 0 ;
}
if (dp.get(mask).get(prev + 1 ) != - 1 ) {
return dp.get(mask).get(prev + 1 );
}
int ans = 10000 ;
for ( int i = 0 ; i < s.length(); i++) {
int id = s.charAt(i) - 'a' ;
if (check(mask, id) == 1 ) {
if (prev == - 1 ) {
ans = Math.min(ans, solve(a, s, n, id, mask ^ ( 1 << id), dp));
}
else {
ans = Math.min(ans, a.get(prev).get(id) + solve(a, s, n, id, mask ^ ( 1 << id), dp));
}
}
}
dp.get(mask).set(prev + 1 , ans);
return ans;
}
public static void generatePermutation( int mask, int n, List<List<Integer>> a, String s) {
List<List<Integer>> dp = new ArrayList<>();
for ( int i = 0 ; i < ( 1 << n) + 5 ; i++) {
List<Integer> row = new ArrayList<>(Arrays.asList( new Integer[n + 5 ]));
Collections.fill(row, - 1 );
dp.add(row);
}
for ( int i = 0 ; i < s.length(); i++) {
int id = s.charAt(i) - 'a' ;
mask |= ( 1 << id);
}
System.out.println(solve(a, s, n, - 1 , mask, dp));
}
}
|
Python3
def check(mask, i):
c = (mask & ( 1 << i))
return c ! = 0
def solve(a, s, n, prev, mask, dp):
if mask = = 0 :
return 0
if dp[mask][prev + 1 ] ! = - 1 :
return dp[mask][prev + 1 ]
ans = 10000
for i in range ( len (s)):
id = ord (s[i]) - ord ( 'a' )
if check(mask, id ):
if prev = = - 1 :
ans = min (ans, solve(a, s, n, id , mask ^ ( 1 << id ), dp))
else :
ans = min (ans, a[prev][ id ] + solve(a, s, n, id , mask ^ ( 1 << id ), dp))
dp[mask][prev + 1 ] = ans
return ans
def generate_permutation(mask, n, a, s):
dp = [[ - 1 for i in range (n + 5 )] for j in range (( 1 << n) + 5 )]
for i in range ( len (s)):
id = ord (s[i]) - ord ( 'a' )
mask | = ( 1 << id )
print (solve(a, s, n, - 1 , mask, dp))
if __name__ = = '__main__' :
N = 5
str = "abcde"
mat = [[ 0 , 5 , 1 , 5 , 3 ],
[ 4 , 0 , 9 , 4 , 2 ],
[ 7 , 9 , 0 , 10 , 7 ],
[ 1 , 2 , 8 , 0 , 2 ],
[ 3 , 9 , 7 , 7 , 0 ]]
generate_permutation( 0 , N, mat, str )
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int Check( int mask, int i)
{
int c = (mask & (1 << i));
return c != 0 ? 1 : 0;
}
static int Solve(List<List< int >> a, string s, int n, int prev, int mask, List<List< int >> dp)
{
if (mask == 0)
{
return 0;
}
if (dp[mask][prev + 1] != -1)
{
return dp[mask][prev + 1];
}
int ans = 10000;
for ( int i = 0; i < s.Length; i++)
{
int id = s[i] - 'a' ;
if (Check(mask, id) == 1)
{
if (prev == -1)
{
ans = Math.Min(ans, Solve(a, s, n, id, mask ^ (1 << id), dp));
}
else
{
ans = Math.Min(ans, a[prev][id] + Solve(a, s, n, id, mask ^ (1 << id), dp));
}
}
}
dp[mask][prev + 1] = ans;
return ans;
}
static void GeneratePermutation( int mask, int n, List<List< int >> a, string s)
{
List<List< int >> dp = new List<List< int >>();
for ( int i = 0; i < (1 << n) + 5; i++)
{
dp.Add( new List< int >());
for ( int j = 0; j < n + 5; j++)
{
dp[i].Add(-1);
}
}
for ( int i = 0; i < s.Length; i++)
{
int id = s[i] - 'a' ;
mask |= (1 << id);
}
Console.WriteLine(Solve(a, s, n, -1, mask, dp));
}
static void Main( string [] args)
{
int N = 5;
string str = "abcde" ;
List<List< int >> mat = new List<List< int >> {
new List< int > {0, 5, 1, 5, 3},
new List< int > {4, 0, 9, 4, 2},
new List< int > {7, 9, 0, 10, 7},
new List< int > {1, 2, 8, 0, 2},
new List< int > {3, 9, 7, 7, 0}
};
GeneratePermutation(0, N, mat, str);
}
}
|
Javascript
function check(mask, i) {
return (mask & (1 << i)) !== 0;
}
function solve(a, s, n, prev, mask, dp) {
if (mask === 0) {
return 0;
}
if (dp[mask][prev + 1] !== -1) {
return dp[mask][prev + 1];
}
let ans = 10000;
for (let i = 0; i < s.length; i++) {
let id = s.charCodeAt(i) - 'a' .charCodeAt(0);
if (check(mask, id)) {
if (prev === -1) {
ans = Math.min(ans, solve(a, s, n, id, mask ^ (1 << id), dp));
}
else {
ans = Math.min(ans, a[prev][id] + solve(a, s, n, id, mask ^ (1 << id), dp));
}
}
}
dp[mask][prev + 1] = ans;
return ans;
}
function generatePermutation(mask, n, a, s) {
let dp = new Array((1 << n) + 5).fill( null ).map(() => new Array(n + 5).fill(-1));
for (let i = 0; i < s.length; i++) {
let id = s.charCodeAt(i) - 'a' .charCodeAt(0);
mask |= (1 << id);
}
console.log(solve(a, s, n, -1, mask, dp));
}
function main() {
let N = 5;
let str = "abcde" ;
let mat = [
[0, 5, 1, 5, 3],
[4, 0, 9, 4, 2],
[7, 9, 0, 10, 7],
[1, 2, 8, 0, 2],
[3, 9, 7, 7, 0]
];
generatePermutation(0, N, mat, str);
}
main();
|
Time Complexity : O(N * 2^N), where N is the size of the string.
Space Auxilitary Complexity : O(2^N * N).
Last Updated :
23 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...