Leetcode 1295. Find Numbers with Even Number of Digits

Problem Explanation:

The problem is asking to count how many numbers in the given list have an even number of digits. For example, if we have the numbers 12 and 345, 12 has 2 digits which is an even number and 345 has 3 digits which is an odd number. Thus, 12 would count towards our total and 345 would not.

This question is solved by iterating over the list of numbers, and for each number, we increment our counter if that number meets the condition of having an even number of digits.

Approach:

The approach is to iterate over all the numbers and increment a counter if that number has an even number of digits. The question utilizes a simple conditional check to count these numbers. It checks for numbers with 2 digits, 4 digits or 5 digits. If a number falls into any of these conditions, we increment our counter. The conditions are:

  • 9 < num && num < 100 for 2 digit numbers
  • 999 < num && num < 10000 for 4 digit numbers
  • num == 100000 for 5 digit numbers which is an edge case

Let's walk through an example to clarify: Suppose we have the following list of numbers [12, 345, 2, 6, 7896]. We initialize a counter to 0. As we iterate over the list:

  • 12 satisfies the first condition 9 < num && num < 100 so we increment the counter: counter = 1
  • 345 does not satisfy any of the conditions so the counter remains the same
  • 2 does not satisfy any of the conditions so the counter remains the same
  • 6 does not satisfy any of the conditions so the counter remains the same
  • 7896 satisfies the second condition 999 < num && num < 10000 so we increment the counter: counter = 2 Finally we return the count as 2.

Python solution:

1
2python
3class Solution:
4    def findNumbers(self, nums):
5        count = 0
6        for num in nums:
7            if 9 < num and num < 100 or 999 < num and num < 10000 or num == 100000:
8                count += 1
9        return count

Java Solution:

1
2java
3public class Solution {
4    public int findNumbers(int[] nums) {
5        int count = 0;
6        for (int num : nums) {
7            if ((9 < num && num < 100) || (999 < num && num < 10000) || num == 100000) {
8                count++;
9            }
10        }
11        return count;
12    }
13}

JavaScript Solution:

1
2javascript
3class Solution {
4    findNumbers(nums) {
5        let count = 0;
6        for(let num of nums) {
7            if (9 < num && num < 100 || 999 < num && num < 10000 || num === 100000) {
8                count ++;
9            }
10        }
11        return count;
12    }
13}

C++ solution:

1
2cpp
3class Solution {
4public:
5    int findNumbers(vector<int>& nums) {
6        int count = 0;
7        for (int num : nums) 
8            if (9 < num && num < 100 || 999 < num && num < 10000 || num == 100000)
9                count++;
10        return count;
11    }
12};

C# Solution:

1
2csharp
3public class Solution {
4    public int FindNumbers(int[] nums) {
5        int count = 0;
6        foreach (int num in nums) {
7            if ((9 < num && num < 100) || (999 < num && num < 10000) || num == 100000) {
8                count++;
9            }
10        }
11        return count;
12    }
13}

In all of these solutions, we iterate through the array nums, and for each number, we check if it satisfies the condition for having an even number of digits. If it does, we increment the count. At the end, we simply return the count.## Go Lang Solution:

1
2golang
3   import "strconv"
4
5    func findNumbers(nums []int) int {
6        count := 0
7        for _, num := range nums {
8            if len(strconv.Itoa(num)) % 2 == 0 {
9                count ++
10            }
11        }
12        return count
13    }

Ruby Solution:

1
2ruby
3def find_numbers(nums)
4    count = 0
5    nums.each do |num|
6        count += 1 if num.to_s.length % 2 == 0
7    end
8    count
9end

Swift Solution:

1
2swift
3class Solution {
4    func findNumbers(_ nums: [Int]) -> Int {
5        var count = 0
6        for num in nums {
7            if (String(num).count % 2 == 0) {
8                count+=1
9            }
10        }
11        return count
12    }
13}

PHP Solution:

1
2php
3function findNumbers($nums) {
4    $count = 0;
5    foreach($nums as $num) {
6        if (strlen((string)$num) % 2 == 0) {
7            $count++;
8        }
9    }
10    return $count;
11}

All of these solutions also follow a similar approach. For each number in the input list or array, we convert the number into a string to get the number of digits. If the length of the string is an even number, we increment our counter. Finally, we return the counter value, which is the number of elements in the array with an even number of digits.


Got a question?ย Ask the Teaching Assistantย anything you don't understand.

Still not clear? Ask in the Forum, ย Discordย orย Submitย the part you don't understand to our editors.

โ†
โ†‘TA ๐Ÿ‘จโ€๐Ÿซ