Wells Fargo | OA 2020 | Min Rotations to find longest common prefix

I was in a coding inteview and the below question was asked. I could only manage to pass 10/19 cases.
Would be really helpful if someone can help!

You are given two strings, String1 and String2 (need not be equal in length)
String1 is fixed. You need to find the minimum rotations (left, right) that will give
the longest common prefix from both the strings. If no such prefix exists, return -1.

Example:
String1: fm47abcde
String2: 47ghijfm

Output: 2

Explaination: The longest common prefix is fm47 and we can get it in String2 by rotating 'm'
to the right and 'f' to the right.

String1: abcde
String2: cabfg

Output: 1 (rotate 'c' left to get LCP "ab")

Note: Incase it's not self-explanatory: Rotations can only be done at either ends. It's not shifts.

Comments (7)