Minimum Rotations for the Longest Common Prefix
Problem statement
You are given two nonempty strings fixed and rotating. The first string never changes.
In one rotation of rotating, move its first character to the end or move its last character to the front. You may perform either operation any number of times.
First maximize the length of the common prefix between fixed and the rotated string. Among all rotations that achieve this maximum length, return the minimum number of rotations. If the maximum common-prefix length is 0, return -1.
Function
minimumRotationsForLongestPrefix(fixed: String, rotating: String) → intExamples
Example 1
fixed = "fm47abcde"rotating = "47ghijfm"return = 2Move m and then f from the end to the front. The rotated string starts with fm47, the longest possible common prefix.
Example 2
fixed = "abcde"rotating = "cabfg"return = 1Move c from the front to the end. The strings then share prefix ab.
Example 3
fixed = "abc"rotating = "xyz"return = -1No rotation starts with a, so no nonempty common prefix exists.
Example 4
fixed = "hello"rotating = "hello"return = 0The unrotated strings already have the longest possible prefix.
Constraints
1 <= fixed.length, rotating.length <= 2 * 10^5.- Both strings contain visible ASCII characters.