(UVA) Letter Frequency - Solution

Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=740&page=show_problem&problem=2624



import java.io.*;
import java.util.*;

class Main {
    public void process() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
      
        int numTests = Integer.parseInt(br.readLine());
        for (int test = 0; test < numTests; test++) {
            String line = br.readLine();
            line = line.toLowerCase();
            int maxFreq = 0;
            int[] frequencies = new int[26];
            // pass through the array to see the frequence of the characters
            for (int i = 0; i < line.length(); i++) {
                if (!Character.isLetter(line.charAt(i))) {
                    continue;
                }
                maxFreq = Math.max(maxFreq, ++frequencies[line.charAt(i)-'a']);
            }
            // get all the characters that appear maxFreq times
            for (int i = 0; i < 26; i++) {
                if (frequencies[i] != maxFreq) {
                    continue;
                }
                bw.write((char)(i+'a'));
            }
            bw.write("\n");
        }
                                             
        bw.flush();
        bw.close();
      
        return;
    }
  
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
      
        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução