给定一个包含小写字符的字符串。任务是打印输入字符串中出现的最大字符。如果两个或多个字符出现的次数相同,请按字典顺序(字母顺序)打印最低(第一个)字符。 例如:
null
输入: 试样 输出: E “t”、“e”和“s”出现两次,但“e”是字典中最小的字符。 输入: 示例程序 输出: A.
在 以前的 文章中,如果在最大时间内出现多个字符,则返回其中任何一个字符。在本文中,将返回所有字符中按词典编纂的最小字符。 方法: 申报 频率[26] 数组,用作哈希表,用于存储输入字符串中每个字符的频率。在字符串中迭代并增加 频率[s[i]] 每一个角色。Don’不要撕开窗户 频率[] 从左到右排列,并跟踪到目前为止频率最高的角色。freq[i]处的值表示字符(i+‘a’)的频率。 以下是上述方法的实施情况:
C++
// C++ implementation to find // the maximum occurring character in // an input string which is lexicographically first #include <bits/stdc++.h> using namespace std; // function to find the maximum occurring character in // an input string which is lexicographically first char getMaxOccurringChar( char str[]) { // freq[] used as hash table int freq[26] = { 0 }; // to store maximum frequency int max = -1; // to store the maximum occurring character char result; // length of 'str' int len = strlen (str); // get frequency of each character of 'str' for ( int i = 0; i < len; i++) freq[str[i] - 'a' ]++; // for each character, where character is obtained by // (i + 'a') check whether it is the maximum character // so far and accodingly update 'result' for ( int i = 0; i < 26; i++) if (max < freq[i]) { max = freq[i]; result = ( char )(i + 'a' ); } // maximum occurring character return result; } // Driver Code int main() { char str[] = "sample program" ; cout << "Maximum occurring character = " << getMaxOccurringChar(str); return 0; } |
JAVA
// Java implementation to find // the maximum occurring character in // an input string which is lexicographically first class GFG { // function to find the maximum occurring character in // an input string which is lexicographically first static char getMaxOccurringChar( char str[]) { // freq[] used as hash table int freq[] = new int [ 26 ]; // to store maximum frequency int max = - 1 ; // to store the maximum occurring character char result = 0 ; // length of 'str' int len = str.length; // get frequency of each character of 'str' for ( int i = 0 ; i < len; i++) { if (str[i] != ' ' ) { freq[str[i] - 'a' ]++; } } // for each character, where character is obtained by // (i + 'a') check whether it is the maximum character // so far and accodingly update 'result' for ( int i = 0 ; i < 26 ; i++) { if (max < freq[i]) { max = freq[i]; result = ( char ) (i + 'a' ); } } // maximum occurring character return result; } // Driver Code public static void main(String[] args) { char str[] = "sample program" .toCharArray(); System.out.println( "Maximum occurring character = " + getMaxOccurringChar(str)); } } // This code is contributed by 29AjayKumar |
Python3
# Python 3 implementation to find the # maximum occurring character in an input # string which is lexicographically first # function to find the maximum occurring # character in an input string which is # lexicographically first def getMaxOccurringChar( str ): # freq[] used as hash table freq = [ 0 for i in range ( 100 )] # to store maximum frequency max = - 1 # to store the maximum occurring # character length of 'str' len__ = len ( str ) # get frequency of each character of 'str' for i in range ( 0 , len__, 1 ): freq[ ord ( str [i]) - ord ( 'a' )] + = 1 # for each character, where character # is obtained by (i + 'a') check whether # it is the maximum character so far and # accodingly update 'result' for i in range ( 26 ): if ( max < freq[i]): max = freq[i] result = chr ( ord ( 'a' ) + i) # maximum occurring character return result # Driver Code if __name__ = = '__main__' : str = "sample program" print ( "Maximum occurring character =" , getMaxOccurringChar( str )) # This code is contributed by # Surendra_Gangwar |
C#
// C# implementation to find // the maximum occurring character in // an input string which is lexicographically first using System; class GFG { // function to find the maximum occurring character in // an input string which is lexicographically first static char getMaxOccurringChar( string str) { // freq[] used as hash table int [] freq = new int [26]; // to store maximum frequency int max = -1; // to store the maximum occurring character char result = ( char )0; // length of 'str' int len = str.Length; // get frequency of each character of 'str' for ( int i = 0; i < len; i++) { if (str[i] != ' ' ) { freq[str[i] - 'a' ]++; } } // for each character, where character is obtained by // (i + 'a') check whether it is the maximum character // so far and accodingly update 'result' for ( int i = 0; i < 26; i++) { if (max < freq[i]) { max = freq[i]; result = ( char ) (i + 'a' ); } } // maximum occurring character return result; } // Driver Code public static void Main() { string str = "sample program" ; Console.WriteLine( "Maximum occurring character = " + getMaxOccurringChar(str)); } } |
Javascript
<script> // Javascript implementation to find // the maximum occurring character in // an input string which is lexicographically first // function to find the maximum occurring character in // an input string which is lexicographically first function getMaxOccurringChar(str) { // freq[] used as hash table let freq = new Array(26); for (let i=0;i<freq.length;i++) { freq[i]=0; } // to store maximum frequency let max = -1; // to store the maximum occurring character let result = 0; // length of 'str' let len = str.length; // get frequency of each character of 'str' for (let i = 0; i < len; i++) { if (str[i] != ' ' ) { freq[str[i].charCodeAt(0) - 'a' .charCodeAt(0)]++; } } // for each character, where character is obtained by // (i + 'a') check whether it is the maximum character // so far and accodingly update 'result' for (let i = 0; i < 26; i++) { if (max < freq[i]) { max = freq[i]; result = String.fromCharCode(i + 'a' .charCodeAt(0)); } } // maximum occurring character return result; } // Driver Code let str= "sample program" .split( "" ); document.write( "Maximum occurring character = " + getMaxOccurringChar(str)); // This code is contributed by rag2127 </script> |
输出:
Maximum occurring character = a
时间复杂性: O(n)。 辅助空间: O(1)。 资料来源: Sabre面试体验|第二套
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END