📌  相关文章
📜  通过重新排列字符来删除字符串字符之间的空格的最低成本

📅  最后修改于: 2021-05-06 10:03:12             🧑  作者: Mango

给定一个由字符和空格组成的字符串str ,任务是找到最小的成本以减少字符串的字符之间的空格数。

例子:

方法:想法是将所有字符移到最靠近字符串中间的位置,以使总成本最小化。步骤如下:

  1. 用0初始化总费用。
  2. 横切字符串并计算两个字符之间的间隔。
  3. 将两个字符放在一起,然后将成本加到总成本中。
  4. 对所有字符重复上述步骤。

下面是上述方法的实现:

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


输出:

4

时间复杂度: O(N)
辅助空间: O(1)