87.5k views
2 votes
PYTHON

Given an array of integers a, your task is to count the number of pairs i and j (where 0 ≤ i < j < a.length), such that a[i] and a[j] are digit anagrams.
Two integers are considered to be digit anagrams if they contain the same digits. In other words, one can be obtained from the other by rearranging the digits (or trivially, if the numbers are equal). For example, 54275 and 45572 are digit anagrams, but 321 and 782 are not (since they don't contain the same digits). 220 and 22 are also not considered as digit anagrams, since they don't even have the same number of digits.
Example
For a = [25, 35, 872, 228, 53, 278, 872], the output should be solution(a) = 4.
There are 4 pairs of digit anagrams:
a[1] = 35 and a[4] = 53 (i = 1 and j = 4),
a[2] = 872 and a[5] = 278 (i = 2 and j = 5),
a[2] = 872 and a[6] = 872 (i = 2 and j = 6),
a[5] = 278 and a[6] = 872 (i = 5 and j = 6).
Input/Output
[execution time limit] 6 seconds (py3)
[input] array.integer a
An array of non-negative integers.
Guaranteed constraints:
1 ≤ a.length ≤ 105,
0 ≤ a[i] ≤ 109.
[output] integer64
The number of pairs i and j, such that a[i] and a[j] are digit anagrams.
def solution(a):
#implement

User Da Tong
by
8.4k points

2 Answers

2 votes

Final answer:

To count the number of pairs of digit anagrams in an array of integers, use a hash table to record the frequency of each digit pattern and iterate through the array to check for anagrams. Return the count of anagram pairs.

Step-by-step explanation:

To count the number of pairs of digit anagrams in an array of integers, you can use a hash table to record the frequency of each digit pattern. Iterate through the array and for each number, sort its digits and use the sorted digits as a key in the hash table. If the key is already present, increment a count variable. Finally, return the count variable, which represents the number of pairs of digit anagrams in the array.

Algorithm:

  1. Create an empty hash table.
  2. Initialize a count variable to 0.
  3. Iterate through the array of integers.
  4. For each number, convert it to a string and sort its digits.
  5. If the sorted digits are already a key in the hash table, increment the count variable by the value associated with the key.
  6. Otherwise, add the sorted digits as a key to the hash table with a value of 0.
  7. After the iteration, return the count variable.

User Aniket Tiratkar
by
8.9k points
6 votes

For each number in the array, convert it to a tuple representing its digit frequencies and use this tuple as a key in the dictionary. Keep a count of how many times each tuple has occurred.

def tuple_from_digits(num):

# Convert the number to a tuple of its digit frequencies

digit_freq = [0] * 10 # 10 digits (0-9)

for digit in str(num):

digit_freq[int(digit)] += 1

return tuple(digit_freq)

def solution(a):

digit_anagrams_count = 0

tuple_count = defaultdict(int)

for num in a:

# Get the tuple representing digit frequencies

digit_tuple = tuple_from_digits(num)

# Update the count of digit anagrams

digit_anagrams_count += tuple_count[digit_tuple]

# Increment the count of this tuple for future occurrences

tuple_count[digit_tuple] += 1

return digit_anagrams_count

# Example usage:

a = [25, 35, 872, 228, 53, 278, 872]

result = solution(a)

print(result) # Output should be 4

User Doerig
by
7.6k points

Related questions

1 answer
2 votes
57.2k views
asked Nov 10, 2024 40.8k views
Eric Rosenberg asked Nov 10, 2024
by Eric Rosenberg
7.0k points
1 answer
5 votes
40.8k views