Minimum Rotations for the Longest Common Prefix
Updated
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.
More Wells Fargo Online Assessment questions
- Server Upgrade PlanningOA · Seen Nov 2026
- Student and Course EnrollmentOA · Seen Nov 2026
- Bond Interest Summary Above 3%OA · Seen Oct 2026
- Count Phrases Formed by Anagram ReplacementOA · Seen Oct 2026
- Event Statistics AggregatorOA · Seen Sep 2026
- Ordered Deployment Configuration ValidatorOA · Seen Sep 2026
- Longest Subsequence That Is a SubstringOA · Seen Jul 2026
- Overloaded Game Account InventoriesOA · Seen Jul 2026
- Count Server ReplacementsOA · PHONE SCREEN · Seen Feb 2026
- Minimum Remaining LengthOA · PHONE SCREEN · Seen Feb 2026
Similar questions at other companies
- Compare Version NumbersInMobi · PHONE SCREENInMobi interview questions
- Polynomial CoefficientsShopify · OAShopify interview questions
- Parse Router LogsIntuit · OAIntuit interview questions
- Simplify a Quadratic ExpressionOpenAI · OAOpenAI interview questions
- String CompressionAbridge · OAAbridge interview questions
- K Smallest SubstringAkuna Capital · OAAkuna Capital interview questions
- Format TextTikTok · OATikTok interview questions
- Next Greater Element IIIThe Walt Disney Company · OAThe Walt Disney Company interview questions
- Counting Binary Substrings (Backend)Canva · OACanva interview questions
- Shortest SubstringRippling · OARippling interview questions