📌  相关文章
📜  执行给定操作后查找出现次数最多的字符

📅  最后修改于: 2021-09-04 11:45:03             🧑  作者: Mango

给定一个由0、1*组成的字符串str ,任务是在执行给定的操作后从01 中找出出现次数最多的字符:

注意:如果执行给定操作后 0 和 1 的频率相同,则打印-1

例子:

方法:产生最终结果字符串的想法,然后比较0和1的频率。 下面是步骤:

  • 计算字符串中 0 和 1 的初始频率,并将它们存储在变量中,例如count_0count_1
  • 初始化一个变量,比如prev ,为 -1。遍历字符串并检查当前字符是否为* 。如果是这样,那么继续。
  • 如果它是遇到的第一个字符并且为0,则将所有*添加到count_0并将prev更改为当前索引。
  • 否则,如果第一个字符为1,则将prev更改为当前索引。
  • 如果前一个字符是1而当前字符是0则在字符之间添加一半*0和一半到1
  • 如果前一个字符为 0,当前字符为1,则它们之间的*字符不能被替换。
  • 如果前一个和当前两个字符的类型相同,则将*的计数添加到频率中。
  • 比较01的频率并打印出现次数最多的字符。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to find the
// maximum occurring character
void solve(string S)
{
    // Initialize count of
    // zero and one
    int count_0 = 0, count_1 = 0;
  
    int prev = -1;
  
    // Iterate over the given string
    for (int i = 0; i < S.length(); i++) {
  
        // Count the zeros
        if (S[i] == '0')
            count_0++;
  
        // Count the ones
        else if (S[i] == '1')
            count_1++;
    }
  
    // Iterate over the given string
    for (int i = 0; i < S.length(); i++) {
  
        // Check if character
        // is * then continue
        if (S[i] == '*')
            continue;
  
        // Check if first character
        // after * is X
        else if (S[i] == '0' && prev == -1) {
  
            // Add all * to
            // the frequency of X
            count_0 = count_0 + i;
  
            // Set prev to the
            // i-th character
            prev = i;
        }
  
        // Check if first character
        // after * is Y
        else if (S[i] == '1' && prev == -1) {
  
            // Set prev to the
            // i-th character
            prev = i;
        }
  
        // Check if prev character is 1
        // and current character is 0
        else if (S[prev] == '1' && S[i] == '0') {
  
            // Half of the * will be
            // converted to 0
            count_0 = count_0 + (i - prev - 1) / 2;
  
            // Half of the * will be
            // converted to 1
            count_1 = count_1 + (i - prev - 1) / 2;
            prev = i;
        }
  
        // Check if prev and current are 1
        else if (S[prev] == '1' && S[i] == '1') {
  
            // All * will get converted to 1
            count_1 = count_1 + (i - prev - 1);
            prev = i;
        }
  
        // No * can be replaced
        // by either 0 or 1
        else if (S[prev] == '0' && S[i] == '1')
  
            // Prev becomes the ith character
            prev = i;
  
        // Check if prev and current are 0
        else if (S[prev] == '0' && S[i] == '0') {
  
            // All * will get converted to 0
            count_0 = count_0 + (i - prev - 1);
            prev = i;
        }
    }
  
    // If frequency of 0
    // is more
    if (count_0 > count_1)
        cout << "0";
  
    // If frequency of 1
    // is more
    else if (count_1 > count_0)
        cout << "1";
  
    else {
        cout << -1;
    }
}
  
// Driver code
int main()
{
    // Given string
    string str = "**0**1***0";
  
    // Function Call
    solve(str);
  
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
  
class GFG{
      
// Function to find the
// maximum occurring character
static void solve(String S)
{
      
    // Initialize count of
    // zero and one
    int count_0 = 0, count_1 = 0;
  
    int prev = -1;
  
    // Iterate over the given string
    for(int i = 0; i < S.length(); i++)
    {
          
        // Count the zeros
        if (S.charAt(i) == '0')
            count_0++;
  
        // Count the ones
        else if (S.charAt(i) == '1')
            count_1++;
    }
  
    // Iterate over the given string
    for(int i = 0; i < S.length(); i++) 
    {
  
        // Check if character
        // is * then continue
        if (S.charAt(i) == '*')
            continue;
  
        // Check if first character
        // after * is X
        else if (S.charAt(i) == '0' && prev == -1)
        {
              
            // Add all * to
            // the frequency of X
            count_0 = count_0 + i;
  
            // Set prev to the
            // i-th character
            prev = i;
        }
  
        // Check if first character
        // after * is Y
        else if (S.charAt(i) == '1' && prev == -1)
        {
  
            // Set prev to the
            // i-th character
            prev = i;
        }
  
        // Check if prev character is 1
        // and current character is 0
        else if (S.charAt(prev) == '1' &&
                 S.charAt(i) == '0') 
        {
  
            // Half of the * will be
            // converted to 0
            count_0 = count_0 + (i - prev - 1) / 2;
  
            // Half of the * will be
            // converted to 1
            count_1 = count_1 + (i - prev - 1) / 2;
            prev = i;
        }
  
        // Check if prev and current are 1
        else if (S.charAt(prev) == '1' &&
                 S.charAt(i) == '1')
        {
  
            // All * will get converted to 1
            count_1 = count_1 + (i - prev - 1);
            prev = i;
        }
  
        // No * can be replaced
        // by either 0 or 1
        else if (S.charAt(prev) == '0' && 
                 S.charAt(i) == '1')
  
            // Prev becomes the ith character
            prev = i;
  
        // Check if prev and current are 0
        else if (S.charAt(prev) == '0' && 
                 S.charAt(i) == '0') 
        {
  
            // All * will get converted to 0
            count_0 = count_0 + (i - prev - 1);
            prev = i;
        }
    }
  
    // If frequency of 0
    // is more
    if (count_0 > count_1)
        System.out.print("0");
  
    // If frequency of 1
    // is more
    else if (count_1 > count_0)
        System.out.print("1");
  
    else 
    {
        System.out.print("-1");
    }
}
  
// Driver code
public static void main (String[] args)
{
      
    // Given string
    String str = "**0**1***0";
  
    // Function call
    solve(str);
}
}
  
// This code is contributed by code_hunt


Python3
# Python3 program for the above approach
  
# Function to find the
# maximum occurring character
def solve(S):
      
    # Initialize count of
    # zero and one
    count_0 = 0
    count_1 = 0
  
    prev = -1
  
    # Iterate over the given string
    for i in range(len(S)) :
  
        # Count the zeros
        if (S[i] == '0'):
            count_0 += 1
  
        # Count the ones
        elif (S[i] == '1'):
            count_1 += 1
      
    # Iterate over the given string
    for i in range(len(S)):
  
        # Check if character
        # is * then continue
        if (S[i] == '*'):
            continue
  
        # Check if first character
        # after * is X
        elif (S[i] == '0' and prev == -1):
  
            # Add all * to
            # the frequency of X
            count_0 = count_0 + i
  
            # Set prev to the
            # i-th character
            prev = i
          
        # Check if first character
        # after * is Y
        elif (S[i] == '1' and prev == -1):
  
            # Set prev to the
            # i-th character
            prev = i
          
        # Check if prev character is 1
        # and current character is 0
        elif (S[prev] == '1' and S[i] == '0'):
  
            # Half of the * will be
            # converted to 0
            count_0 = count_0 + (i - prev - 1) / 2
  
            # Half of the * will be
            # converted to 1
            count_1 = count_1 + (i - prev - 1) // 2
            prev = i
          
        # Check if prev and current are 1
        elif (S[prev] == '1' and S[i] == '1'):
  
            # All * will get converted to 1
            count_1 = count_1 + (i - prev - 1)
            prev = i
          
        # No * can be replaced
        # by either 0 or 1
        elif (S[prev] == '0' and S[i] == '1'):
  
            # Prev becomes the ith character
            prev = i
  
        # Check if prev and current are 0
        elif (S[prev] == '0' and S[i] == '0'):
  
            # All * will get converted to 0
            count_0 = count_0 + (i - prev - 1)
            prev = i
          
    # If frequency of 0
    # is more
    if (count_0 > count_1):
        print("0")
  
    # If frequency of 1
    # is more
    elif (count_1 > count_0):
        print("1")
    else:
        print("-1")
      
# Driver code
  
# Given string
str = "**0**1***0"
  
# Function call
solve(str)
  
# This code is contributed by code_hunt


C#
// C# program for the above approach 
using System;
  
class GFG{
  
// Function to find the
// maximum occurring character
static void solve(string S)
{
      
    // Initialize count of
    // zero and one
    int count_0 = 0, count_1 = 0;
  
    int prev = -1;
  
    // Iterate over the given string
    for(int i = 0; i < S.Length; i++)
    {
          
        // Count the zeros
        if (S[i] == '0')
            count_0++;
  
        // Count the ones
        else if (S[i] == '1')
            count_1++;
    }
  
    // Iterate over the given string
    for(int i = 0; i < S.Length; i++)
    {
          
        // Check if character
        // is * then continue
        if (S[i] == '*')
            continue;
  
        // Check if first character
        // after * is X
        else if (S[i] == '0' && prev == -1) 
        {
  
            // Add all * to
            // the frequency of X
            count_0 = count_0 + i;
  
            // Set prev to the
            // i-th character
            prev = i;
        }
  
        // Check if first character
        // after * is Y
        else if (S[i] == '1' && prev == -1)
        {
              
            // Set prev to the
            // i-th character
            prev = i;
        }
  
        // Check if prev character is 1
        // and current character is 0
        else if (S[prev] == '1' && S[i] == '0')
        {
              
            // Half of the * will be
            // converted to 0
            count_0 = count_0 + (i - prev - 1) / 2;
  
            // Half of the * will be
            // converted to 1
            count_1 = count_1 + (i - prev - 1) / 2;
            prev = i;
        }
  
        // Check if prev and current are 1
        else if (S[prev] == '1' && S[i] == '1')
        {
              
            // All * will get converted to 1
            count_1 = count_1 + (i - prev - 1);
            prev = i;
        }
  
        // No * can be replaced
        // by either 0 or 1
        else if (S[prev] == '0' && S[i] == '1')
  
            // Prev becomes the ith character
            prev = i;
  
        // Check if prev and current are 0
        else if (S[prev] == '0' && S[i] == '0')
        {
              
            // All * will get converted to 0
            count_0 = count_0 + (i - prev - 1);
            prev = i;
        }
    }
  
    // If frequency of 0
    // is more
    if (count_0 > count_1)
        Console.Write("0");
  
    // If frequency of 1
    // is more
    else if (count_1 > count_0)
        Console.Write("1");
  
    else
    {
        Console.Write("-1");
    }
}
  
// Driver code
public static void Main ()
{
      
    // Given string
    string str = "**0**1***0";
  
    // Function call
    solve(str);
}
}
  
// This code is contributed by code_hunt


输出:
0

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live