Lanqiao Cup official website basic questions (word analysis)

Question description

Xiao Lan is learning a magical language. The words in this language are all composed of lowercase English letters. Some words are very long, far exceeding the length of normal English words. Xiao Lan couldn't remember some words after learning them for a long time. He was going to stop memorizing these words completely and instead distinguish words based on which letters appeared most frequently in the words.

Now, please help Xiaolan. After giving him a word, help him find the letter that appears the most and the number of times this letter appears.

Enter description

Enter a line containing a word consisting only of lowercase English letters.

For all evaluation cases, the input word length does not exceed 1000.

Output description

Output two lines. The first line contains an English letter, indicating which letter appears most frequently in the word. If there are multiple letters appearing the same number of times, output the one with the smallest lexicographic order.

The second line contains an integer representing the number of times the most common letter appears in the word.

Input and output samples

Example 1

enter

lanqiao

output

a
2

Example 2

enter

longlonglongistoolong

output

o
6

 

operating restrictions

  • Maximum running time: 1s
  • Maximum running memory: 256M

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        char maxchar='a';
        int ans=0;
        int[] count=new int[26];
        String s=scan.nextLine();
        for(int i=0;i<s.length();i++){
          count[s.charAt(i)-'a']++;
        }
        int max=count[0];
        for(int i=0;i<26;i++){
          if(max<count[i]){
            max=count[i];
            maxchar=(char)('a'+i);
          }
        }
        System.out.println(maxchar);
        System.out.println(max);
        scan.close();
    }
}

Guess you like

Origin blog.csdn.net/s44Sc21/article/details/131730754#comments_28192853