📜  字符串满足给定条件所需的最小更改

📅  最后修改于: 2021-04-22 07:30:43             🧑  作者: Mango

给定一个二进制字符串str 。在一次操作中,我们可以将任何‘1’更改为‘0’或将任何‘0’更改为‘1’ 。我们的任务是在字符串进行最少的更改,这样,如果我们使用字符串的任何前缀,则1的数量应大于或等于0的数量。

例子:

方法:可以贪婪地解决问题。字符串的第一个字符必须为1 。然后,我们通过字符和检查字符串的字符遍历如果所需条件满足与否,如果没有的话,我们增加所需的更改数字符串的其余部分。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the minimum
// changes required
int minChanges(string str, int n)
{
  
    // To store the count of minimum changes,
    // number of ones and the number of zeroes
    int count = 0, zeros = 0, ones = 0;
  
    // First character has to be '1'
    if (str[0] != '1') {
        count++;
        ones++;
    }
  
    for (int i = 1; i < n; i++) {
        if (str[i] == '0')
            zeros++;
        else
            ones++;
  
        // If condition fails
        // changes need to be made
        if (zeros > ones) {
            zeros--;
            ones++;
            count++;
        }
    }
  
    // Return the required count
    return count;
}
  
// Driver code
int main()
{
    string str = "0000";
    int n = str.length();
    cout << minChanges(str, n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG 
{
  
// Function to return the minimum 
// changes required 
static int minChanges(char[] str, int n) 
{ 
  
    // To store the count of minimum changes, 
    // number of ones and the number of zeroes 
    int count = 0, zeros = 0, ones = 0; 
  
    // First character has to be '1' 
    if (str[0] != '1')
    { 
        count++; 
        ones++; 
    } 
  
    for (int i = 1; i < n; i++)
    { 
        if (str[i] == '0') 
            zeros++; 
        else
            ones++; 
  
        // If condition fails 
        // changes need to be made 
        if (zeros > ones) 
        { 
            zeros--; 
            ones++; 
            count++; 
        } 
    } 
  
    // Return the required count 
    return count; 
} 
  
// Driver code 
public static void main(String[] args)
{
    char []str = "0000".toCharArray(); 
    int n = str.length; 
    System.out.print(minChanges(str, n)); 
}
}
  
// This code has been contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
  
# Function to return the minimum
# changes required
def minChanges(str, n):
      
    # To store the count of minimum changes,
    # number of ones and the number of zeroes
    count, zeros, ones = 0, 0, 0
  
    # First character has to be '1'
    if (ord(str[0])!= ord('1')):
        count += 1
        ones += 1
  
    for i in range(1, n):
        if (ord(str[i]) == ord('0')):
            zeros += 1
        else:
            ones += 1
  
        # If condition fails
        # changes need to be made
        if (zeros > ones):
            zeros -= 1
            ones += 1
            count += 1
  
    # Return the required count
    return count
  
# Driver code
if __name__ == '__main__':
    str = "0000"
    n = len(str)
    print(minChanges(str, n))
  
# This code contributed by PrinciRaj1992


C#
// C# implementation of the approach
using System;
  
class GFG 
{
  
// Function to return the minimum 
// changes required 
static int minChanges(char[] str, int n) 
{ 
  
    // To store the count of minimum changes, 
    // number of ones and the number of zeroes 
    int count = 0, zeros = 0, ones = 0; 
  
    // First character has to be '1' 
    if (str[0] != '1')
    { 
        count++; 
        ones++; 
    } 
  
    for (int i = 1; i < n; i++)
    { 
        if (str[i] == '0') 
            zeros++; 
        else
            ones++; 
  
        // If condition fails 
        // changes need to be made 
        if (zeros > ones) 
        { 
            zeros--; 
            ones++; 
            count++; 
        } 
    } 
  
    // Return the required count 
    return count; 
} 
  
// Driver code 
public static void Main(String[] args)
{
    char []str = "0000".ToCharArray(); 
    int n = str.Length; 
    Console.Write(minChanges(str, n)); 
}
}
  
// This code contributed by Rajput-Ji


PHP
 $ones) 
        {
            $zeros--;
            $ones++;
            $count++;
        }
    }
  
    // Return the required count
    return $count;
}
  
// Driver code
$str = "0000";
$n = strlen($str);
echo minChanges($str, $n);
  
// This code is contributed by mits
?>


输出:
2