Escriba una función eficiente para devolver el máximo de caracteres que aparecen en la string de entrada, por ejemplo, si la string de entrada es «prueba», la función debería devolver ‘t’.
Algoritmo:
un enfoque obvio para resolver este problema sería ordenar la string de entrada y luego atravesar la string ordenada para encontrar el carácter que aparece la mayor cantidad de veces. Veamos si podemos mejorar en esto. Entonces usaremos una técnica llamada ‘Hashing’. En esto, cuando atravesamos la string, dividiríamos cada carácter en una array de caracteres ASCII.
C++
// C++ program to output the maximum occurring character // in a string #include<bits/stdc++.h> #define ASCII_SIZE 256 using namespace std; char getMaxOccurringChar(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 " << getMaxOccurringChar(str); }
Java
// Java program to output the maximum occurring character // in a string public class GFG { static final int ASCII_SIZE = 256; static char getMaxOccurringChar(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.charAt(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.charAt(i)]) { max = count[str.charAt(i)]; result = str.charAt(i); } } return result; } // Driver Method public static void main(String[] args) { String str = "sample string"; System.out.println("Max occurring character is " + getMaxOccurringChar(str)); } }
Python3
# Python program to return the maximum occurring character in the input string ASCII_SIZE = 256 def getMaxOccurringChar(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",getMaxOccurringChar(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 getMaxOccurringChar(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 " + getMaxOccurringChar(str)); } } // This code is contributed by Sam007
PHP
<?php // PHP program to output the maximum // occurring character in a string $ASCII_SIZE = 256; function getMaxOccurringChar($str) { global $ASCII_SIZE; // Create array to keep the count // of individual characters and // initialize the array as 0 $count = array_fill(0, $ASCII_SIZE, NULL); // Construct character count array // from the input string. $len = strlen($str); $max = 0; // Initialize max count // Traversing through the string // and maintaining the count of // each character for ($i = 0; $i < ($len); $i++) { $count[ord($str[$i])]++; if ($max < $count[ord($str[$i])]) { $max = $count[ord($str[$i])]; $result = $str[$i]; } } return $result; } // Driver Code $str = "sample string"; echo "Max occurring character is " . getMaxOccurringChar($str); // This code is contributed by ita_c ?>
Javascript
<script> // Javascript program to output the maximum occurring character // in a string let ASCII_SIZE = 256; function getMaxOccurringChar(str) { // Create array to keep the count of individual // characters and initialize the array as 0 let count = new Array(ASCII_SIZE); for (let i = 0; i < ASCII_SIZE; i++) { count[i] = 0; } // Construct character count array from the input // string. let len = str.length; for (let i = 0; i < len; i++) { count[str[i].charCodeAt(0)] += 1; } let max = -1; // Initialize max count let result = ' '; // Initialize result // Traversing through the string and maintaining // the count of each character for (let i = 0; i < len; i++) { if (max < count[str[i].charCodeAt(0)]) { max = count[str[i].charCodeAt(0)]; result = str[i]; } } return result; } // Driver Method let str = "sample string"; document.write("Max occurring character is " , getMaxOccurringChar(str)); // This code is contributed by avanitrachhadiya2155 </script>
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA