Codeforces 236A Boy or Girl Solution & Explanation

Difficulty : 800

Problem Description

Those days, many boys use beautiful girls’ photos as avatars in forums. So it is pretty hard to tell the gender of a user at the first glance. Last year, our hero went to a forum and had a nice chat with a beauty (he thought so). After that they talked very often and eventually they became a couple in the network.

But yesterday, he came to see “her” in the real world and found out “she” is actually a very strong man! Our hero is very sad and he is too tired to love again now. So he came up with a way to recognize users’ genders by their user names.

This is his method: if the number of distinct characters in one’s user name is odd, then he is a male, otherwise she is a female. You are given the string that denotes the user name, please help our hero to determine the gender of this user by his method.

Input

The first line contains a non-empty string, that contains only lowercase English letters — the user name. This string contains at most 100 letters.

Output

If it is a female by our hero’s method, print “CHAT WITH HER!” (without the quotes), otherwise, print “IGNORE HIM!” (without the quotes).

Examples

Inputwjmzbmr
OutputCHAT WITH HER!
Inputxiaodao
OutputIGNORE HIM!
Inputsevenkplus
OutputCHAT WITH HER!

Note

For the first example. There are 6 distinct characters in “wjmzbmr”. These characters are: “w”, “j”, “m”, “z”, “b”, “r”. So wjmzbmr is a female and you should print “CHAT WITH HER!”.

Understand this problem in 3 seconds:

Given a string, check the number of its distinct characters. If the count is even, print ‘CHAT WITH HER!’; otherwise, print ‘IGNORE HIM!’.


Solution

In our first solution, we use HashSet to store the distinct characters and easily check their count.

C# Solution

Solution1 – HashSet

string username = Console.ReadLine();
HashSet<char> hSet = new HashSet<char>();

char[] charArr = username.ToCharArray();

foreach (char c in charArr)
{
    hSet.Add(c);
}

string res = hSet.Count%2==0? "CHAT WITH HER!" : "IGNORE HIM!";

Console.WriteLine(res);

Solution2 – LINQ Distinct

string username = Console.ReadLine();

int cnt = username.Distinct().Count();

string res = cnt%2==0? "CHAT WITH HER!" : "IGNORE HIM!";

Console.WriteLine(res);

A more easy way is to use LINQ built-in function distinct.

Java Solution

Solution1

import java.util.HashSet;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String username = scanner.nextLine();
        HashSet<Character> hSet = new HashSet<>();

        char[] charArr = username.toCharArray();

        for (char c : charArr) {
            hSet.add(c);
        }

        String res = hSet.size() % 2 == 0 ? "CHAT WITH HER!" : "IGNORE HIM!";
        System.out.println(res);
    }
}

Solution2 – streaming

import java.util.Scanner;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String username = scanner.nextLine();
        long cnt = username.chars()
                .mapToObj(c -> (char) c)
                .distinct()
                .count();

        String res = cnt % 2 == 0 ? "CHAT WITH HER!" : "IGNORE HIM!";
        System.out.println(res);
    }
}

Similar as C#’s LINQ, Java can use it’s own streaming to distinct and count easily

Python3 Solution

username = input()
s = set()

for u in username:
    s.add(u)
    
res = "CHAT WITH HER!" if len(s)%2==0  else "IGNORE HIM!"
print(res);

JavaScript Solution

username = readline();

var s = new Set();
for(var u of username){
    s.add(u);
}

var res = s.size % 2 === 0 ? "CHAT WITH HER!" : "IGNORE HIM!";
print(res);

Conclusion

Believe me, don’t trust this weird way to guess the gender of anyone !

🧡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 – 236A – Codeforces

Random Codeforces Posts

Leave a Reply

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