Skip to content

1295. Find Numbers with Even Number of Digits | LeetCode | Python | Solution

Here is the problem link to see the problem details. The problem description is very concise and easy to understand.

Given an array of integers, we have to return how many of them contain an even number of digits.

Example of an even number of digits:
1234 = even (here are four digits and four is an even number)
123 = not even

First, we will declare a variable to store the even number of digits. The initial value of this variable will be 0. After that, we will loop through the array to check each of the elements. To check a number is even or odd, we will convert it into a string and use Len function to count the string length. If the length is divisible by 2, we will increase our variable value by one.

After the loop, we will return the variable.

The code will explain much better than my explanation. See the solution below.

class Solution:
    def findNumbers(self, nums: List[int]) -> int:
        
        count = 0
        
        for num in nums:
            if len(str(num)) % 2 == 0:
                count += 1
        
        return count

If you submit the code in LeetCode, it will be accepted.

Published inLeetCode

Comments are closed.