Description
Solutions
Cycle Shift
🤘 INTERN

Feel free to checkout the image source below for the original problem statement 🐰

In a world of numbers, a special kind of magic exists called the "cyclic shift." This mystical operation involves taking some digits from the end of a number and placing them at the beginning, while all the other digits move one step forward in perfect order. When performed correctly, this can transform one number into another. Now, imagine you are given two numbers, a and b, both with the same number of digits. These numbers are called "cyclic pairs" if, after performing a cyclic shift on a, it can become identical to b. You can shift none, some, or all the digits from the end of a to the front, and if it becomes equal to b, the pair is magical. Your quest is to take an array filled with positive integers and find how many such magical cyclic pairs exist. Specifically, you are tasked with counting all pairs i and j (where 0 ≤ i < j < a.length) such that the numbers a[i] and a[j] share the same number of digits, and one can become the other by performing a cyclic shift. How many of these hidden cyclic pairs will you discover on your journey through the array of numbers? The answer lies in your hands!

Example 1:

Input:  a = [13, 5604, 31, 2, 13, 4560, 546, 654, 456]
Output: 3
Explanation:
Feel free to checkout the image source below for the original problem statement 🐿️ Once upon a time, there was a curious list of numbers: a = [13, 5604, 31, 2, 13, 4560, 546, 654, 456]. This list held a little secret—hidden within were pairs of numbers that could be transformed into each other through a magical process called cyclic shifting. Our clever heroes noticed that five pairs of numbers in the list could be perfectly matched through this shifting trick. For example, the number 13 could be shifted around in a circle, but it always remained the same as another 13! However, some numbers couldn't match no matter how much they shifted, like 546 and 456. Even though 654 was a magical twin to 546, 456 remained an outsider since it was too small to complete the shift. Another interesting case came when 4560 tried to make friends with 456, but alas, they were just too different—one had four digits, while the other had only three. And so, with the magic of cyclic shifts, the heroes discovered three perfect matches in this list!
Constraints:
  • 1 <= years.length <= 100
  • 1 <= years[i] <= 104
Thumbnail 0
Thumbnail 1
Testcase

Result
Case 1

input:

output: