Given an integer W and an array a[] of size 26 where ai denotes the cost of using the ith alphabet, the task is to find lexicographically the largest string that can be generated for a cost, W.
Examples:
Input: W = 236, a[] = {1, 1, 2, 33, 4, 6, 9, 7, 36, 32, 58, 32, 28, 904, 22, 255, 47, 69, 558, 544, 21, 36, 48, 85, 48, 58}
Output: zzzze
Explanation:
4 * (cost of z) + cost of e = 4 * 58 + 4 = 232 + 4 = 236
Input: W = 6674, a[] = {252, 288, 578, 746, 295, 884, 198, 655, 503, 868, 942, 506, 718, 498, 727, 338, 43, 768, 783, 312, 369, 712, 230, 106, 102, 554}
Output: zzzzzzzzzzzyyyyqqqq
Explanation:
11 * (cost of z) + 4 * (cost of y) + 4 * (cost of q) = 11 * 554 + 4 * 102 + 4 * 43 = 6094 + 408 + 172 = 6674
Approach: The idea is to iterate over the array a[] from the characters ‘z‘ to ‘a‘ alphabets. Now, find a combination in a[] where the sum is equal to W. The same repeated number may be chosen from a[] an unlimited number of times. Then, use recursion and backtracking to solve the problem. Follow the steps below to solve the problem:
- For the subproblem sum = 0 at any instant, print the string obtained as the cost of the characters stored in the array till now sums up to W and since the string has been generated appending characters starting from ‘z‘, the string thus formed is the lexicographically the largest string.
- Otherwise, if the sum is negative or index < 0, return false for these subproblems.
- Otherwise, find lexicographically the largest string possible by including as well as excluding the current character in the resultant string.
- Print lexicographically the largest string obtained by completing the above steps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool lexi_largest_String( int a[], int i,
int sum, vector< int > &v)
{
if (sum < 0)
return false ;
if (sum == 0)
return true ;
if (i < 0)
return false ;
v.push_back(i);
if (lexi_largest_String(a, i,
sum - a[i], v))
return true ;
auto it = v.end();
it--;
v.erase(it);
return lexi_largest_String(a, i - 1,
sum, v);
}
void generateString( int a[], int sum)
{
vector< int > v;
lexi_largest_String(a, 25,
sum, v);
string s = "" ;
for ( int j = 0; j < v.size(); j++)
s += ( char )(v[j] + 'a' );
cout << s << endl;
}
int main()
{
int a[] = {1, 1, 2, 33, 4, 6, 9,
7, 36, 32, 58, 32, 28,
904, 22, 255, 47, 69,
558, 544, 21, 36, 48,
85, 48, 58};
int sum = 236;
generateString(a, sum);
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean lexi_largest_String( int a[], int i,
int sum,
Vector<Integer> v)
{
if (sum < 0 )
return false ;
if (sum == 0 )
return true ;
if (i < 0 )
return false ;
v.add(i);
if (lexi_largest_String(a, i, sum -
a[i], v))
return true ;
v.remove(v.size() - 1 );
return lexi_largest_String(a, i - 1 ,
sum, v);
}
static void generateString( int a[],
int sum)
{
Vector<Integer> v = new Vector<Integer>();
lexi_largest_String(a, 25 , sum, v);
String s = "" ;
for ( int j = 0 ; j < v.size(); j++)
s += ( char )(v.get(j) + 'a' );
System.out.print(s + "\n" );
}
public static void main(String[] args)
{
int a[] = { 1 , 1 , 2 , 33 , 4 , 6 , 9 ,
7 , 36 , 32 , 58 , 32 , 28 ,
904 , 22 , 255 , 47 , 69 ,
558 , 544 , 21 , 36 , 48 ,
85 , 48 , 58 };
int sum = 236 ;
generateString(a, sum);
}
}
|
Python3
def lexi_largest_string(a, i, sum , v):
if ( sum < 0 ):
return False
if ( sum = = 0 ):
return True
if (i < 0 ):
return False
v.append(i)
if (lexi_largest_string(a, i,
sum - a[i], v)):
return True
v.pop( - 1 )
return lexi_largest_string(a, i - 1 ,
sum , v)
def generateString(a, sum ):
v = []
lexi_largest_string(a, 25 , sum , v)
s = ""
for j in range ( len (v)):
s + = chr (v[j] + ord ( 'a' ))
print (s)
if __name__ = = '__main__' :
a = [ 1 , 1 , 2 , 33 , 4 , 6 , 9 ,
7 , 36 , 32 , 58 , 32 , 28 ,
904 , 22 , 255 , 47 , 69 ,
558 , 544 , 21 , 36 , 48 ,
85 , 48 , 58 ]
sum = 236
generateString(a, sum )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool lexi_largest_String( int []a, int i,
int sum, List< int > v)
{
if (sum < 0)
return false ;
if (sum == 0)
return true ;
if (i < 0)
return false ;
v.Add(i);
if (lexi_largest_String(a, i, sum -
a[i], v))
return true ;
v.RemoveAt(v.Count - 1);
return lexi_largest_String(a, i - 1,
sum, v);
}
static void generateString( int []a,
int sum)
{
List< int > v = new List< int >();
lexi_largest_String(a, 25, sum, v);
String s = "" ;
for ( int j = 0; j < v.Count; j++)
s += ( char )(v[j] + 'a' );
Console.Write(s + "\n" );
}
public static void Main(String[] args)
{
int []a = {1, 1, 2, 33, 4, 6, 9,
7, 36, 32, 58, 32, 28,
904, 22, 255, 47, 69,
558, 544, 21, 36, 48,
85, 48, 58};
int sum = 236;
generateString(a, sum);
}
}
|
Javascript
<script>
function lexi_largest_String(a, i, sum, v)
{
if (sum < 0)
return false ;
if (sum == 0)
return true ;
if (i < 0)
return false ;
v.push(i);
if (lexi_largest_String(a, i, sum - a[i], v))
return true ;
v.pop();
return lexi_largest_String(a, i - 1, sum, v);
}
function generateString(a, sum)
{
let v = [];
lexi_largest_String(a, 25, sum, v);
let s = "" ;
for (let j = 0; j < v.length; j++)
s += String.fromCharCode(v[j] + 'a' .charCodeAt());
document.write(s + "</br>" );
}
let a = [1, 1, 2, 33, 4, 6, 9,
7, 36, 32, 58, 32, 28,
904, 22, 255, 47, 69,
558, 544, 21, 36, 48,
85, 48, 58];
let sum = 236;
generateString(a, sum);
</script>
|
Time Complexity: O(226)
Auxiliary Space: O(N)