FastPrepCyclic Shift Pairs

Cyclic Shift Pairs

Roblox logoRoblox● MediumINTERNOA
Learn

Problem statement

A cyclic shift of a positive integer moves zero or more digits from the end of its decimal representation to the beginning, while preserving the relative order of all digits. For example, the cyclic shifts of 546 are 546, 654, and 465.

Two positive integers form a cyclic pair if they have the same number of digits and one can be transformed into the other by a cyclic shift.

Given an array of positive integers a, return the number of index pairs (i, j) such that:

  • 0 <= i < j < a.length;
  • a[i] and a[j] contain the same number of digits; and
  • a[i] is a cyclic shift of a[j].

Function

solution(a: int[]) → int

Examples

Example 1

a = [13, 5604, 31, 2, 13, 4560, 546, 654, 456]return = 5

There are 5 cyclic pairs:

  • a[0] = 13 and a[2] = 31;
  • a[0] = 13 and a[4] = 13;
  • a[1] = 5604 and a[5] = 4560;
  • a[2] = 31 and a[4] = 13; and
  • a[6] = 546 and a[7] = 654.

By contrast, 546 and 456 are not a cyclic pair: the cyclic shifts of 546 are only 546, 654, and 465.

Also, 4560 and 456 are not a cyclic pair because they contain different numbers of digits.

Constraints

  • a contains only positive integers.
  • Each value is interpreted using its standard decimal representation, without leading zeros.

More Roblox problems

See Roblox hiring insights
public int solution(int[] a) {
  // Write your code here.
}
a[13, 5604, 31, 2, 13, 4560, 546, 654, 456]
expected5
Checking account…