编写一个有效的函数来返回输入字符串出现最多的字符,例如,如果输入字符串是“test”,那么函数应该返回 ‘t’。
算法:
解决此问题的一种明显方法是对输入字符串进行排序,然后遍历已排序的字符串以查找出现次数最多的字符。让我们看看我们是否可以改进这一点。因此,我们将使用一种称为“哈希”的技术。在这里,当我们遍历字符串,我们会将每个字符散列到一个 ASCII字符数组中。
Input string = “test”
1: Construct character count array from the input string.
count['e'] = 1
count['s'] = 1
count['t'] = 2
2: Return the index of maximum value in count array (returns ‘t’).
通常,ASCII字符是 256,所以我们使用我们的 Hash 数组大小为 256。但是如果我们知道我们的输入字符串将只有从 0 到 127 的值的字符,我们可以将 Hash 数组大小限制为 128。类似地,基于额外已知有关输入字符串,哈希数组大小可以限制为 26。
执行:
C++
// C++ program to output the maximum occurring character
// in a string
#include
#define ASCII_SIZE 256
using namespace std;
char getMaxOccuringChar(char* str)
{
// Create array to keep the count of individual
// characters and initialize the array as 0
int count[ASCII_SIZE] = {0};
// Construct character count array from the input
// string.
int len = strlen(str);
int max = 0; // Initialize max count
char result; // Initialize result
// Traversing through the string and maintaining
// the count of each character
for (int i = 0; i < len; i++) {
count[str[i]]++;
if (max < count[str[i]]) {
max = count[str[i]];
result = str[i];
}
}
return result;
}
// Driver program to test the above function
int main()
{
char str[] = "sample string";
cout << "Max occurring character is "
<< getMaxOccuringChar(str);
}
Java
// Java program to output the maximum occurring character
// in a string
public class GFG
{
static final int ASCII_SIZE = 256;
static char getMaxOccuringChar(String str)
{
// Create array to keep the count of individual
// characters and initialize the array as 0
int count[] = new int[ASCII_SIZE];
// Construct character count array from the input
// string.
int len = str.length();
for (int i=0; i
Python
# Python program to return the maximum occurring character in the input string
ASCII_SIZE = 256
def getMaxOccuringChar(str):
# Create array to keep the count of individual characters
# Initialize the count array to zero
count = [0] * ASCII_SIZE
# Utility variables
max = -1
c = ''
# Traversing through the string and maintaining the count of
# each character
for i in str:
count[ord(i)]+=1;
for i in str:
if max < count[ord(i)]:
max = count[ord(i)]
c = i
return c
# Driver program to test the above function
str = "sample string"
print "Max occurring character is " + getMaxOccuringChar(str)
# Although this program can be written in atmost 3 lines in Python
# the above program has been written for a better understanding of
# the reader
# Shorter version of the program
# import collections
# str = "sample string"
# print "Max occurring character is " +
# collections.Counter(str).most_common(1)[0][0]
# This code has been contributed by Bhavya Jain
C#
// C# program to output the maximum
// occurring character in a string
using System;
class GFG
{
static int ASCII_SIZE = 256;
static char getMaxOccuringChar(String str)
{
// Create array to keep the count of
// individual characters and
// initialize the array as 0
int []count = new int[ASCII_SIZE];
// Construct character count array
// from the input string.
int len = str.Length;
for (int i = 0; i < len; i++)
count[str[i]]++;
int max = -1; // Initialize max count
char result = ' '; // Initialize result
// Traversing through the string and
// maintaining the count of each character
for (int i = 0; i < len; i++) {
if (max < count[str[i]]) {
max = count[str[i]];
result = str[i];
}
}
return result;
}
// Driver Method
public static void Main()
{
String str = "sample string";
Console.Write("Max occurring character is " +
getMaxOccuringChar(str));
}
}
// This code is contributed by Sam007
PHP
Javascript
输出:
Max occurring character is s
时间复杂度: O(n)
空间复杂度: O(1) — 因为我们使用固定空间(哈希数组),而不管输入字符串的大小。
笔记:
如果多个字符具有相同的计数并且该计数最大,则该函数返回输入字符串具有最大计数的第一个字符。例如,如果输入字符串是“测试样本”,则有三个字符具有相同的最大计数为两个,即“t”、“e”和“s”,但我们的程序将产生“t”,因为“t”在输入字符串在第一位.同样,“cbbbbccc”的输出将是“c”。
作为上述程序的一种变体,如果您想为输入“测试样本”输出“e”,即最大计数的字符,并且该字符应该是最小的ASCII值,请考虑代码的变化。对于“cbbbbccc”,输出应为“b”。试试看 !
另外,如果我们可以避免上面的两个循环,你能想到改进吗?基本上,您需要弄清楚我们是否可以用一个循环本身而不是两个循环来解决同样的问题。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。