给定一个由字符和空格组成的字符串str ,任务是找到最小成本来减少字符串 的字符之间的空格数。
Cost of moving a character for index i to index j is defined as: | i – j |
例子:
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.
方法:想法是将所有字符移动到最靠近字符串中间的位置,从而使总体成本最小化。以下是步骤:
- 将总成本初始化为 0。
- 遍历字符串并计算两个字符之间的空格。
- 将两个字符放在一起并将成本添加到总成本中。
- 对所有字符重复上述步骤。
下面是上述方法的实现:
C++
// C++ program to gather characters
// of a string in minimum cost
#include
using namespace std;
// Function to calculate the
// minimum cost
int min_cost(string S)
{
// Stores the minimum cost
int cost = 0;
// Stores the count of
// characters found
int F = 0;
// Stores the count of
// blank spaces found
int B = 0;
int count = 0;
for(char c : S)
if(c == ' ')
count++;
// Stores the count of
// total characters
int n = S.size() - count;
// If the count of characters
// is equal to 1
if (n == 1)
return cost;
// Iterate over the string
for(char in : S)
{
// Consider the previous character
// together with current character
if (in != ' ')
{
// If not together already
if (B != 0)
{
// Add the cost to group
// them together
cost += min(n - F, F) * B;
B = 0;
}
// Increase count of
// characters found
F += 1;
}
// Otherwise
else
{
// Increase count of
// spaces found
B += 1;
}
}
// Return the total
// cost obtained
return cost;
}
// Driver Code
int main ()
{
string S = " @ $";
cout << min_cost(S);
return 0;
}
// This code is contributed by Amit Katiyar
Java
// Java program to gather characters
// of a string in minimum cost
import java.util.*;
import java.lang.*;
class GFG{
// Function to calculate the
// minimum cost
static int min_cost(String S)
{
// Stores the minimum cost
int cost = 0;
// Stores the count of
// characters found
int F = 0;
// Stores the count of
// blank spaces found
int B = 0;
int count = 0;
for(char c : S.toCharArray())
if(c == ' ')
count++;
// Stores the count of
// total characters
int n = S.length() - count;
// If the count of characters
// is equal to 1
if (n == 1)
return cost;
// Iterate over the string
for(char in : S.toCharArray())
{
// Consider the previous character
// together with current character
if (in != ' ')
{
// If not together already
if (B != 0)
{
// Add the cost to group
// them together
cost += Math.min(n - F, F) * B;
B = 0;
}
// Increase count of
// characters found
F += 1;
}
// Otherwise
else
{
// Increase count of
// spaces found
B += 1;
}
}
// Return the total
// cost obtained
return cost;
}
// Driver Code
public static void main (String[] args)
{
String S = " @ $";
System.out.println(min_cost(S));
}
}
// This code is contributed by offbeat
Python3
# Python3 program to gather characters
# of a string in minimum cost
# Function to calculate the
# minimum cost
def min_cost(S):
# Stores the minimum cost
cost = 0
# Stores the count of
# characters found
F = 0
# Stores the count of
# blank spaces found
B = 0
# Stores the count of
# total characters
n = len(S)-S.count(' ')
# If the count of characters
# is equal to 1
if n == 1:
return cost
# Iterate over the string
for char in S:
# Consider the previous character
# together with current character
if char != ' ':
# If not together already
if B != 0:
# Add the cost to group
# them together
cost += min(n - F, F) * B
B = 0
# Increase count of
# characters found
F += 1
# Otherwise
else:
# Increase count of
# spaces found
B += 1
# Return the total
# cost obtained
return cost
# Driver Code
S = " @ $"
print(min_cost(S))
C#
// C# program to gather characters
// of a string in minimum cost
using System;
class GFG{
// Function to calculate the
// minimum cost
static int min_cost(String S)
{
// Stores the minimum cost
int cost = 0;
// Stores the count of
// characters found
int F = 0;
// Stores the count of
// blank spaces found
int B = 0;
int count = 0;
foreach(char c in S.ToCharArray())
if(c == ' ')
count++;
// Stores the count of
// total characters
int n = S.Length - count;
// If the count of characters
// is equal to 1
if (n == 1)
return cost;
// Iterate over the string
foreach(char inn in S.ToCharArray())
{
// Consider the previous character
// together with current character
if (inn != ' ')
{
// If not together already
if (B != 0)
{
// Add the cost to group
// them together
cost += Math.Min(n - F, F) * B;
B = 0;
}
// Increase count of
// characters found
F += 1;
}
// Otherwise
else
{
// Increase count of
// spaces found
B += 1;
}
}
// Return the total
// cost obtained
return cost;
}
// Driver Code
public static void Main(String[] args)
{
String S = " @ $";
Console.WriteLine(min_cost(S));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
4
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live