Minimum cost to remove the spaces between characters of a String by rearranging the characters
Last Updated :
07 May, 2021
Given a string str that consists of characters and spaces, the task is to find the minimum cost to reduce the number of spaces between characters of the string.
Cost of moving a character for index i to index j is defined as: | i – j |
Examples:
Input: str = ” @ $”
Output: 4
Explanation:
As the characters are at indices [2, 7] (only two), either move the first character to the nearest second character or vice-versa. The cost required is |2-6| = 4 or |6-2| = 4.
Therefore, the minimum cost is 4.
Input: str = ” A ”
Output: 0
Explanation:
Since the string consists of only one character, no changes required. Therefore, minimum cost is 0.
Approach: The idea is to move all the characters nearest to the middle of the string so that the overall cost is minimized. Below are the steps:
- Initialize the total cost with 0.
- Transverse the string and count the spaces between the two characters.
- Get both the characters together and add the cost to the total cost.
- Repeat the above steps for all characters.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int min_cost(string S)
{
int cost = 0;
int F = 0;
int B = 0;
int count = 0;
for ( char c : S)
if (c == ' ' )
count++;
int n = S.size() - count;
if (n == 1)
return cost;
for ( char in : S)
{
if (in != ' ' )
{
if (B != 0)
{
cost += min(n - F, F) * B;
B = 0;
}
F += 1;
}
else
{
B += 1;
}
}
return cost;
}
int main ()
{
string S = " @ $" ;
cout << min_cost(S);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG{
static int min_cost(String S)
{
int cost = 0 ;
int F = 0 ;
int B = 0 ;
int count = 0 ;
for ( char c : S.toCharArray())
if (c == ' ' )
count++;
int n = S.length() - count;
if (n == 1 )
return cost;
for ( char in : S.toCharArray())
{
if (in != ' ' )
{
if (B != 0 )
{
cost += Math.min(n - F, F) * B;
B = 0 ;
}
F += 1 ;
}
else
{
B += 1 ;
}
}
return cost;
}
public static void main (String[] args)
{
String S = " @ $" ;
System.out.println(min_cost(S));
}
}
|
Python3
def min_cost(S):
cost = 0
F = 0
B = 0
n = len (S) - S.count( ' ' )
if n = = 1 :
return cost
for char in S:
if char ! = ' ' :
if B ! = 0 :
cost + = min (n - F, F) * B
B = 0
F + = 1
else :
B + = 1
return cost
S = " @ $"
print (min_cost(S))
|
C#
using System;
class GFG{
static int min_cost(String S)
{
int cost = 0;
int F = 0;
int B = 0;
int count = 0;
foreach ( char c in S.ToCharArray())
if (c == ' ' )
count++;
int n = S.Length - count;
if (n == 1)
return cost;
foreach ( char inn in S.ToCharArray())
{
if (inn != ' ' )
{
if (B != 0)
{
cost += Math.Min(n - F, F) * B;
B = 0;
}
F += 1;
}
else
{
B += 1;
}
}
return cost;
}
public static void Main(String[] args)
{
String S = " @ $" ;
Console.WriteLine(min_cost(S));
}
}
|
Javascript
<script>
function min_cost(S)
{
let cost = 0;
let F = 0;
let B = 0;
let count = 0;
for (let i in S)
if (S[i] == ' ' )
count++;
let n = S.length - count;
if (n == 1)
return cost;
for (let i in S)
{
if (S[i] != ' ' )
{
if (B != 0)
{
cost += Math.min(n - F, F) * B;
B = 0;
}
F += 1;
}
else
{
B += 1;
}
}
return cost;
}
let S = " @ $" ;
document.write(min_cost(S.split( '' )));
</script>
|
Output:
4
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...