4
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 digitAnagrams(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] 4 seconds (php)
[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.
5
You are given an array of strings arr. Your task is to construct a string from the words in arr, starting with the 0th character from each word (in the order they appear in arr), followed by the 1st character, then the 2nd character, etc. If one of the words doesn't have an ith character, skip that word.
Return the resulting string.
Example
For arr = ["Daisy", "Rose", "Hyacinth", "Poppy"], the output should be readingVertically(arr) = "DRHPaoyoisapsecpyiynth".
First, we append all 0th characters and obtain string "DRHP";
Then we append all 1st characters and obtain string "DRHPaoyo";
Then we append all 2nd characters and obtain string "DRHPaoyoisap";
Then we append all 3rd characters and obtain string "DRHPaoyoisapaecp";
Then we append all 4th characters and obtain string "DRHPaoyoisapaecpyiy";
Finally, only letters in the arr[2] are left, so we append the rest characters and get "DRHPaoyoisapaecpyiynth";
example
For arr = ["E", "M", "I", "L", "Y"], the output should be readingVertically(arr) = "EMILY".
Since each of these strings have only one character, the answer will be concatenation of each string in order, so the answer is EMILY.
Input/Output
[execution time limit] 4 seconds (php)
[input] array.string arr
An array of strings containing alphanumeric characters.
Guaranteed constraints:
1 ≤ arr.length ≤ 100,
1 ≤ arr[i].length ≤ 100.
[output] string
Return the resulting string.
6
Given an integer n and an array a of length n, your task is to apply the following mutation to a:
Array a mutates into a new array b of length n.
For each i from 0 to n - 1, b[i] = a[i - 1] + a[i] + a[i + 1].
If some element in the sum a[i - 1] + a[i] + a[i + 1] does not exist, it should be set to 0. For example, b[0] should be equal to 0 + a[0] + a[1].
Example
For n = 5 and a = [4, 0, 1, -2, 3], the output should be mutateTheArray(n, a) = [4, 5, -1, 2, 1].
b[0] = 0 + a[0] + a[1] = 0 + 4 + 0 = 4
b[1] = a[0] + a[1] + a[2] = 4 + 0 + 1 = 5
b[2] = a[1] + a[2] + a[3] = 0 + 1 + (-2) = -1
b[3] = a[2] + a[3] + a[4] = 1 + (-2) + 3 = 2
b[4] = a[3] + a[4] + 0 = (-2) + 3 + 0 = 1
So, the resulting array after the mutation will be [4, 5, -1, 2, 1].
Input/Output
[execution time limit] 4 seconds (php)
[input] integer n
An integer representing the length of the given array.
Guaranteed constraints:
1 ≤ n ≤ 103.
[input] array.integer a
An array of integers that needs to be mutated.
Guaranteed constraints:
a.length = n,
-103 ≤ a[i] ≤ 103.
[output] array.integer
The resulting array after the mutation.