Count Access Code Pairs
Learn this problemProblem 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) → longExamples
Example 1
fragments = [1, 212, 12, 12]accessCode = 1212return = 3The 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 = 2The 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 = 6Any 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
fragmentsis a positive integer. accessCodeis a positive integer.
More Tiktok problems
- Count Key ChangesOA · Seen Jul 2026
- Travel Distance on ScootersOA · Seen Jul 2026
- Count Skipped Numbers After SubtractionsOA · Seen Jul 2026
- Obstacle Placement QueriesOA · Seen Jul 2026
- Repeated Grouped Digit SumOA · Seen Jul 2026
- Count Cyclic Digit PairsOA · Seen Jun 2026
- Event ID Check Completion TimesOA · Seen Jun 2026
- Check Even-Position MonotonicityOA · Seen Jun 2026