📌  相关文章
📜  给定字符串中每个索引的最大重复字符

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

给定由小写字母组成的字符串str ,任务是找到为字符串 的每个字符获得的最大重复字符。如果对于任何索引,超过一个字符出现的次数达到最大值,则打印最近出现的字符。

例子:

方法:按照下面给出的步骤解决问题:

  • 初始化一个数组,比如freq[]来存储字符串中每个不同字符的频率。
  • 初始化两个变量,比如maxcharMax分别存储最大重复字符和最大重复字符的频率。
  • 遍历字符串并更新当前字符的频率,更新maxcharMax的值
  • 最后,在每个字符遍历后打印charMaxmax的值。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
  
// Function to print the
// maximum repeating
// character at each index
// of the String
void findFreq(string str,
              int N)
{
  // Stores frequency of
  // each distinct character
  int freq[256];
   
  memset(freq, 0,
         sizeof(freq));
 
  // Stores frequency of
  // maximum repeating
  // character
  int max = 0;
 
  // Stores the character having
  // maximum frequency
  char charMax = '0';
 
  // Traverse the String
  for (int i = 0; i < N; i++)
  {
    // Stores current character
    char ch = str[i];
 
    // Update the frequency of str[i]
    freq[ch]++;
 
    // If frequency of current
    // character exceeds max
    if (freq[ch] >= max)
    {
      // Update max
      max = freq[ch];
 
      // Update charMax
      charMax = ch;
    }
 
    // Print the required output
    cout<< charMax << "->" <<
           max << endl;
  }
}
 
// Driver Code
int main()
{
  string str = "abbc";
 
  // Stores length of str
  int N = str.size();
 
  findFreq(str, N);
}
 
// This code is contributed by Rajput-Ji


Java
// Java program to implement
// the above approach
 
import java.util.*;
 
public class GFG {
 
    // Function to print the maximum repeating
    // character at each index of the string
    public static void findFreq(String str,
                                int N)
    {
 
        // Stores frequency of
        // each distinct character
        int[] freq = new int[256];
 
        // Stores frequency of maximum
        // repeating character
        int max = 0;
 
        // Stores the character having
        // maximum frequency
        char charMax = '0';
 
        // Traverse the string
        for (int i = 0; i < N; i++) {
 
            // Stores current character
            char ch = str.charAt(i);
 
            // Update the frequency of str[i]
            freq[ch]++;
 
            // If frequency of current
            // character exceeds max
            if (freq[ch] >= max) {
 
                // Update max
                max = freq[ch];
 
                // Update charMax
                charMax = ch;
            }
 
            // Print the required output
            System.out.println(
                charMax + " -> " + max);
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String str = "abbc";
 
        // Stores length of str
        int N = str.length();
 
        findFreq(str, N);
    }
}


Python3
# Python3 program to implement
# the above approach
 
# Function to print the maximum repeating
# character at each index of the strring
def findFreq(strr, N):
     
    # Stores frequency of
    # each distinct character
    freq = [0] * 256
 
    # Stores frequency of maximum
    # repeating character
    max = 0
 
    # Stores the character having
    # maximum frequency
    charMax = '0'
 
    # Traverse the strring
    for i in range(N):
         
        # Stores current character
        ch = ord(strr[i])
 
        # Update the frequency of strr[i]
        freq[ch] += 1
 
        # If frequency of current
        # character exceeds max
        if (freq[ch] >= max):
 
            # Update max
            max = freq[ch]
 
            # Update charMax
            charMax = ch
 
        # Print the required output
        print(chr(charMax), "->", max)
 
# Driver Code
if __name__ == '__main__':
     
    strr = "abbc"
 
    # Stores length of strr
    N = len(strr)
 
    findFreq(strr, N)
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to print the maximum repeating
// character at each index of the string
public static void findFreq(String str,
                            int N)
{
  // Stores frequency of
  // each distinct character
  int[] freq = new int[256];
 
  // Stores frequency of maximum
  // repeating character
  int max = 0;
 
  // Stores the character having
  // maximum frequency
  char charMax = '0';
 
  // Traverse the string
  for (int i = 0; i < N; i++)
  {
    // Stores current character
    char ch = str[i];
 
    // Update the frequency of
    // str[i]
    freq[ch]++;
 
    // If frequency of current
    // character exceeds max
    if (freq[ch] >= max)
    {
      // Update max
      max = freq[ch];
 
      // Update charMax
      charMax = ch;
    }
 
    // Print the required output
    Console.WriteLine(charMax +
                      " -> " + max);
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  String str = "abbc";
 
  // Stores length of str
  int N = str.Length;
 
  findFreq(str, N);
}
}
 
// This code is contributed by shikhasingrajput


输出:
a -> 1
b -> 1
b -> 2
b -> 2

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

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