UVA Problem 499 – What’s The Frequency, Kenneth Solution

UVA Problem 499 – What’s The Frequency, Kenneth Solution:


Click here to go to this problem in uva Online Judge.

Solving Technique:

This problem is quite easy. Just avoid the gibberish on top and read the Input Output section.

At first glance looking at the input and output doesn’t make much sense. But if we read carefully or take a look at the instructions carefully we see they have print the frequency for upper and lower case letter.

Your mission should you choose to accept it is regardless of a letter being uppercase or lowercase find the maximum frequency ( occurrence ) of a letter. Then if multiple letters have the same maximum frequency print the uppercase letters with max frequency first. Next print the lowercase letters with max frequency. Now separated by a space print the maximum frequency ( occurrence ) count.

Avoid any characters other than uppercase and lowercase letters. We only work with uppercase and lowercase letters for this problem. 

Happy coding 🙂

Important:  Be sure to add or print a new line after each output. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.


Input:

When riding your bicycle backwards down a one-way street, if the
wheel falls of a canoe, how many ball bearings does it take to fill
up a water buffalo?
Hello Howard.

Output:

e 6
al 7
a 3
Hlo 2

Code:

/*
 * Author: Quickgrid ( Asif Ahmed )
 * Site: https://quickgrid.wordpress.com
 * Problem: uva 499 ( What's The Frequency, Kenneth )
 */

#include<stdio.h>

static char s[256];

int main(){
    register unsigned i;
    while(gets(s)){
        /*
         * Declare array for counting upper and lower case letters
         */
        unsigned upcase[32] = {0}, lowcase[32] = {0};

        for (i = 0; s[i]; ++i){
            /*
             * Check if upper case letter as the input may contain other characters
             */
            if (s[i] >= 'A' && s[i] <= 'Z')
                /*
                 * Subtracting a character ASCII ( upper and lower case ) from its base ASCII will give us an index from 0 to 26
                 */
                ++upcase[s[i] - 'A'];
            else if (s[i] >= 'a' && s[i] <= 'z')
                ++lowcase[s[i] - 'a'];
        }

        /*
         * Set the first index as max then compare against all array to find maximum occurrence of a letter
         */
        unsigned max = upcase[0];

        for (i = 0; i < 26; ++i){
            if (upcase[i] > max)
                max = upcase[i];
            if (lowcase[i] > max)
                max = lowcase[i];
        }

        for (i = 0; i < 26; ++i){
            if (upcase[i] == max)
                 /*
                  * Print the uppercase letters first since it was specified in the problem
                  */
                printf("%c", 'A'+i);
        }
        for (i = 0; i < 26; ++i){
            if (lowcase[i] == max)
                /*
                 * Now print the uppercase letters
                 */
                printf("%c", 'a'+i);
        }

        /*
         * Now print their frequency which is the max frequency
         */
        printf(" %u\n", max);
    }
    return 0;
}

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s