Minimize cost to empty a given string by removing characters alphabetically
Last Updated :
19 Oct, 2021
Given string str, the task is to minimize the total cost to remove all the characters from the string in alphabetical order.
The cost of removing any character at i th index from the string will be i. The indexing is 1-based.
Examples:
Input: str = “abcab”
Output: 8
Explanation:
First char ‘a’ at index 1 is removed, str[] becomes “bcab”,
Then char ‘a’ with index 3 is removed, str[] becomes “bcb”
After that char ‘b’ with index 1 is removed, str[] becomes “cb”,
Then char ‘b’ with index 2 is removed, str[] becomes “c”,
Finally, char ‘c’ is removed.
Total points = 1+3 + 1 + 2 + 1 = 8.
Input: str = “def”
Output: 3
Naive Approach: The simplest approach is to remove the smallest character with a smaller index in the string in each step and keep on adding the cost to the total cost. Print the final cost after this operation.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by precomputing for each character, the number of smaller characters preceding it in the given string. Below are the steps:
- Initialize the total cost to 0.
- Transverse the given string and for each character, count the number of characters that are less than the current character and have occurred before it.
- If this count is 0, this means that the current character will be removed at the present index, so add the index of the character to the resultant cost.
- Else subtract the count from its current index and then add it to the total cost.
- Print the total cost after all the above steps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <vector>
using namespace std;
int minSteps(string str, int N)
{
int smaller, cost = 0;
int f[26] = { 0 };
for ( int i = 0; i < N; i++) {
int curr_ele = str[i] - 'a' ;
smaller = 0;
for ( int j = 0; j <= curr_ele; j++) {
if (f[j])
smaller += f[j];
}
if (smaller == 0)
cost += (i + 1);
else
cost += (i - smaller + 1);
f[str[i] - 'a' ]++;
}
return cost;
}
int main()
{
string str = "abcab" ;
int N = str.size();
cout << minSteps(str, N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int minSteps(String str, int N)
{
int smaller, cost = 0 ;
int f[] = new int [ 26 ];
for ( int i = 0 ; i < N; i++)
{
int curr_ele = str.charAt(i) - 'a' ;
smaller = 0 ;
for ( int j = 0 ; j <= curr_ele; j++)
{
if (f[j] != 0 )
smaller += f[j];
}
if (smaller == 0 )
cost += (i + 1 );
else
cost += (i - smaller + 1 );
f[str.charAt(i) - 'a' ]++;
}
return cost;
}
public static void main(String[] args)
{
String str = "abcab" ;
int N = str.length();
System.out.println(minSteps(str, N));
}
}
|
Python3
def minSteps( str , N):
cost = 0
f = [ 0 ] * 26
for i in range (N):
curr_ele = ord ( str [i]) - ord ( 'a' )
smaller = 0
for j in range (curr_ele + 1 ):
if (f[j]):
smaller + = f[j]
if (smaller = = 0 ):
cost + = (i + 1 )
else :
cost + = (i - smaller + 1 )
f[ ord ( str [i]) - ord ( 'a' )] + = 1
return cost
str = "abcab"
N = len ( str )
print (minSteps( str , N))
|
C#
using System;
class GFG{
static int minSteps( string str, int N)
{
int smaller, cost = 0;
int [] f = new int [26];
for ( int i = 0; i < N; i++)
{
int curr_ele = str[i] - 'a' ;
smaller = 0;
for ( int j = 0; j <= curr_ele; j++)
{
if (f[j] != 0)
smaller += f[j];
}
if (smaller == 0)
cost += (i + 1);
else
cost += (i - smaller + 1);
f[str[i] - 'a' ]++;
}
return cost;
}
public static void Main()
{
string str = "abcab" ;
int N = str.Length;
Console.Write(minSteps(str, N));
}
}
|
Javascript
<script>
function minSteps(str, N)
{
var smaller,
cost = 0;
var f = new Array(26).fill(0);
for ( var i = 0; i < N; i++)
{
var curr_ele = str[i].charCodeAt(0) -
"a" .charCodeAt(0);
smaller = 0;
for ( var j = 0; j <= curr_ele; j++)
{
if (f[j] !== 0)
smaller += f[j];
}
if (smaller === 0)
cost += i + 1;
else
cost += i - smaller + 1;
f[str[i].charCodeAt(0) -
"a" .charCodeAt(0)]++;
}
return cost;
}
var str = "abcab" ;
var N = str.length;
document.write(minSteps(str, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(26)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...