Problem · Array

Count Access Code Pairs

Learn this problem
MediumTiktokNEW GRADINTERNOA
See Tiktok hiring insights

Problem statement

A spy agency has intercepted fragments of a secret code. Each fragment is represented as a positive integer in an array called fragments. Intelligence suggests that the complete access code is a specific number called accessCode.

Your mission is to determine how many different ways the fragments can be paired to form the complete access code. The agency's cryptography rules state that:

  • When fragments are combined, they are simply placed next to each other (not added mathematically)
  • Each specific pair of fragments (by position in the array) counts as a unique combination
  • The fragments can be used in any order, but each fragment position can only be used once in a combination

Practice clarification

For the callable version, write an arrangement as an ordered pair of distinct indices (i, j). It forms the access code when the decimal representation of fragments[i] followed by the decimal representation of fragments[j] equals accessCode. If both orders form accessCode, count both arrangements.

Return the total number of valid arrangements.

Function

countAccessCodePairs(fragments: int[], accessCode: int) → long

Examples

Example 1

fragments = [1, 212, 12, 12]accessCode = 1212return = 3

The valid ordered index pairs are (0, 1), because 1 followed by 212 gives 1212, and (2, 3) plus (3, 2), because the two distinct positions containing 12 can be used in either order. The answer is 3.

Example 2

fragments = [12, 121, 21]accessCode = 12121return = 2

The pair (0, 1) forms 12 followed by 121, and (1, 2) forms 121 followed by 21. Both concatenations equal 12121, so the answer is 2.

Example 3

fragments = [2, 2, 2]accessCode = 22return = 6

Any two different positions form 22. There are 3 choices for the first position and 2 remaining choices for the second, giving 3 * 2 = 6 ordered pairs.

Constraints

  • Every value in fragments is a positive integer.
  • accessCode is a positive integer.

More Tiktok problems

drafts saved locally
public long countAccessCodePairs(int[] fragments, int accessCode) {
    // write your code here
}
fragments[1, 212, 12, 12]
accessCode1212
expected3
checking account