Cyclic Shift Pairs
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]anda[j]contain the same number of digits; anda[i]is a cyclic shift ofa[j].
Function
solution(a: int[]) → intExamples
Example 1
a = [13, 5604, 31, 2, 13, 4560, 546, 654, 456]return = 5There are 5 cyclic pairs:
a[0] = 13anda[2] = 31;a[0] = 13anda[4] = 13;a[1] = 5604anda[5] = 4560;a[2] = 31anda[4] = 13; anda[6] = 546anda[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
acontains only positive integers.- Each value is interpreted using its standard decimal representation, without leading zeros.