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.

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] 4 seconds (js)

[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.

[JavaScript] Syntax Tips

// Prints help message to the console

// Returns a string

function helloWorld(name) {

    console.log(“This prints to the console when you Run Tests”);

    return “Hello, ” + name;

}

Solution:


    function mergeStringsSolution( s1, s2)
    {
        // Stores length of string s1
        var leng1 = s1.length;
    
        // Stores length of string s2
        var leng2 = s2.length;
    
        // Pointer to beginning
        // of string1 i.e., s1
        var out1 = 0;
    
        // Pointer to beginning
        // of string2 i.e., s2
        var out2 = 0;
    
        // Stores the final string
        var result = "";
    
        // Traverse the strings
        while (out1 < leng1 && out2 < leng2) {
    
            // Append the smaller of the
            // two current characters
            if (s1[out1] < s2[out2]) {
                result += s1[out1];
                out1++;
            }
    
            else {
                result += s2[out2];
                out2++;
            }
        }
    
        // Append the remaining characters
        // of any of the two strings
        if (out1 < leng1) {
            result += s1.substr(out1, leng1);
        }
        if (out2 < leng2) {
            result += s2.substr(out2, leng2);
        }
    
        // Print the final string
        console.log(result);
    }
    
    // Driver Code
    var S1 = "super";
    var S2 = "tower";
    // Function Call
    mergeStringsSolution(S1, S2);
    
    // output is : stouperwer
    

About admin

Check Also

You are given a two-digit integer n. Return the sum of its digits.

You are given a two-digit integer n. Return the sum of its digits. Example For …

Leave a Reply

Your email address will not be published.