mediumArraysPattern: pattern recognition and rotation

Array Rotation Alignment Solution

Problem Statement

Given two arrays of integers `sequenceA` and `sequenceB`, determine if `sequenceB` can be transformed into `sequenceA` by applying a series of rotations. If possible, find the minimum number of rotations required.

Examples

Example 1:
Input:{"seq1":[1,2,3,4,5],"seq2":[3,4,5,1,2]}
Output:{"identical":true,"rotations":2}
Explanation: Rotate seq2 by 2 positions to match seq1
Example 2:
Input:{"seq1":[1,2,3,4,5],"seq2":[5,4,3,2,1]}
Output:{"identical":false,"rotations":0}
Explanation: No rotation can make seq1 and seq2 identical

Constraints

  • The sequences will have a length between 2 and 100 elements.
  • All elements in the sequences are integers between 1 and 1000.
Time: O(n) Space: O(n)
The optimal approach involves using the fact that if two sequences can be made identical by rotation, then one must be a rotation of the other. We can check this by concatenating one sequence with itself and checking if the other sequence is a substring of the concatenated sequence.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.