Problem Β· String

Count Cyclic Pairs

Learn this problem
● MediumDatabricksINTERNOA

Problem statement

Two integers form a cyclic pair when they have the same number of decimal digits and one number can be obtained from the other by cyclically rotating its digits. A rotation may move any prefix to the end; zero rotations are allowed.

Given a, return the number of index pairs (i, j) with i < j for which a[i] and a[j] form a cyclic pair.

Function

solution(a: int[]) β†’ int

Examples

Example 1

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

The valid pairs are the three pairs among 13, 31, 13, plus (5604,4560) and (546,654). Numbers with different digit lengths cannot form a pair.

Constraints

🌱

More Databricks problems

drafts saved locally
public int solution(int[] a) {
  // write your code here
}
a[13, 5604, 31, 2, 13, 4560, 546, 654, 456]
expected5
checking account