Codeforces 59A Word Solution & Explanation

Difficulty : 800

Problem Description

Vasya is very upset that many people on the Net mix uppercase and lowercase letters in one word. That’s why he decided to invent an extension for his favorite browser that would change the letters’ register in every word so that it either only consisted of lowercase letters or, vice versa, only of uppercase ones. At that as little as possible letters should be changed in the word. For example, the word HoUse must be replaced with house, and the word ViP — with VIP. If a word contains an equal number of uppercase and lowercase letters, you should replace all the letters with lowercase ones. For example, maTRIx should be replaced by matrix. Your task is to use the given method on one given word.

Input

The first line contains a word s — it consists of uppercase and lowercase Latin letters and possesses the length from 1 to 100.

Output

Print the corrected word s. If the given word s has strictly more uppercase letters, make the word written in the uppercase register, otherwise – in the lowercase one.

Eamples

InputHoUse
Outputhouse
InputViP
OutputVIP
InputmaTRIx
Outputmatrix

Solution

If the count of uppercase letters is greater than the count of lowercase letters, we need to convert the entire word to uppercase; otherwise, the entire word will be converted to lowercase.

So, if the counts are the same, it will definitely be converted to lowercase.

C# Solution

Solution1

string word = Console.ReadLine();
int uCnt = 0;   //UpperCase letter count
int lCnt = 0;   //LowerCase letter count

foreach(var c in word){
    if(char.IsUpper(c)){
        uCnt+=1;
    }
    else {
        lCnt+=1;
    }
}

Console.WriteLine( uCnt>lCnt ? word.ToUpper() : word.ToLower());

Java Solution

Solution1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String word = scanner.nextLine();
        int uCnt = 0;   //UpperCase letter count
        int lCnt = 0;   //LowerCase letter count
        
        for (char c : word.toCharArray()) {
            if (Character.isUpperCase(c)) {
                uCnt+=1;
            } else {
                lCnt+=1;
            }
        }
        
        System.out.print(uCnt>lCnt ? word.toUpperCase() : word.toLowerCase());
        
    }
}

Conclusion

In this problem, you will learn how to utilize methods like char.IsUpper to determine the case of a character.

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 59A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *