Similar Problems

Similar Problems not available

Check If Number Has Equal Digit Count And Digit Value - Leetcode Solution

Companies:

LeetCode:  Check If Number Has Equal Digit Count And Digit Value Leetcode Solution

Difficulty: Easy

Topics: string hash-table  

Problem Statement: Given an array of integers nums, return true if every element has an equal number of digits than digit itself, otherwise return false.

Example 1: Input: nums = [555,901,482,1771] Output: false Explanation: The first element (555) has three digits and an equal number of digit values (5), but the second element (901) has three digits and a different number of digit values (0,1,9), and thus is not valid. Similarly, the third element (482) has three digits and a different number of digit values (2, 4, 8), and thus is not valid. Finally, the fourth element (1771) has four digits and an equal number of digit values (1, 7), but the number of digit values is not equal to the number of digits itself, and thus is not valid.

Example 2: Input: nums = [6,24] Output: true Explanation: The first element (6) has one digit and an equal number of digit values (6), and the second element (24) has two digits and two digit values (2, 4), and thus is valid.

Solution Approach: We can solve this problem by iterating through each number in the array. For each number, we can determine its digit count and the number of different digits that it contains. If the digit count equals the number of different digits, the number is valid, and we can move on to the next number. If any number is found invalid, we can terminate the loop and return false. Otherwise, if all numbers are valid, we can return true.

Algorithm:

  1. For each number in the array nums, perform the following steps:
  2. Convert the number to a string and determine the length of the string. This will give us the digit count.
  3. Convert the number to a set of its digits. The length of the set will give us the number of different digits.
  4. If the digit count equals the number of different digits, move on to the next number.
  5. If any number is found to be invalid, terminate the loop and return false.
  6. If all numbers are valid, return true.

Solution: def check_digit_count(nums): for num in nums: # Convert the number to a string and determine its length digit_count = len(str(num))

    # Convert the number to a set of its digits and determine the length of the set
    digit_set = set(str(num))
    digit_value_count = len(digit_set)

    # If the digit count equals the number of different digits, move on to the next number
    if digit_count == digit_value_count:
        continue

    # If any number is found to be invalid, terminate the loop and return false
    return False

# If all numbers are valid, return true
return True

Time Complexity: The time complexity of this solution is O(n*k) where n is the number of integers in the input array and k is the maximum number of digits in any of the integers.

Space Complexity: The space complexity of this solution is O(k) where k is the maximum number of digits in any of the integers. This is because we create a set of digits for each integer, which can have at most k elements.

Check If Number Has Equal Digit Count And Digit Value Solution Code

1