Codeforces 110A Nearly Lucky Number Solution & Explanation

Difficulty : 800

Problem Description

Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 477444 are lucky and 517467 are not.

Unfortunately, not all numbers are lucky. Petya calls a number nearly lucky if the number of lucky digits in it is a lucky number. He wonders whether number n is a nearly lucky number.

Input

The only line contains an integer n (1 ≤ n ≤ 1018).

Please do not use the %lld specificator to read or write 64-bit numbers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.

Output

Print on the single line “YES” if n is a nearly lucky number. Otherwise, print “NO” (without the quotes).

Eamples

Input40047
OutputNO
Input7747774
OutputYES
Input1000000000000000000
OutputNO

Solution

We can use an integer to count the occurrences of lucky numbers in the input string, and by iterating through the input string, we can easily solve this problem.

C# Solution

Solution1

string word = Console.ReadLine();
int cnt = 0;

foreach(var c in word){
    if(c=='4' || c=='7')
    {
        cnt+=1;
    }
}

Console.WriteLine((cnt==4 || cnt ==7) ? "YES" : "NO");

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 cnt = 0;
        
        for (char c : word.toCharArray()) {
            if(c=='4' || c=='7')
            {
                cnt+=1;
            }
        }
        
        System.out.print((cnt==4 || cnt ==7) ? "YES" : "NO");
        
    }
}

Python3 Solution

Solution1

word = input()
cnt = 0

for c in word:
    if(c=='4' or c=='7'):
        cnt+=1
       
print( "YES" if (cnt==4 or cnt==7) else "NO" )

Conclusion

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

Random Codeforces Posts

Leave a Reply

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