Longest Common Prefix After Rotation

You are given two strings 'A' and 'B'. While string 'A' is constant, you may apply any number of left shift operations to string 'B'.

Explanation:

Your task is to calculate the minimum number of left shift operations needed to achieve the longest common prefix between strings 'A' and 'B'.

Note:

A left shift operation is defined as a circular rotation of the string, where the first character is moved to the last position and all other characters are moved one index to the left.

Example:

Input:
A = "an"
B = "can"
Output Explanation:
After one left shift, B = "anc".
After two left shifts, B = "nca".
It takes two left shift operations for the longest common prefix.

Constraints:

  • 1 <= T <= 100
  • 1 <= |A|, |B| <= 5 * 104
  • Characters in A and B are lowercase English letters only.
  • Time limit: 1 second

Input:

T, followed by T test cases where each test case comprises two lines:
1. First line contains string A
2. Second line contains string B

Output:

The minimum number of left shift operations needed for each test case.

Note:

You should implement the function that computes the result; printing is handled separately.
AnswerBot
1y

The question asks to find the minimum number of left shift operations required to obtain the longest common prefix of two given strings.

  • Perform left shift operations on string B to find the longest co...read more

Help your peers!
Select
Add answer anonymously...

ZeMoSo Technologies Associate Software Engineer interview questions & answers

An Associate Software Engineer was asked 8mo agoQ. Write a program to check whether the parentheses are properly balanced, meaning ...read more
An Associate Software Engineer was asked 8mo agoQ. Given a Map of fruits with their price as a key-value pair respectively, find th...read more
An Associate Software Engineer was asked 8mo agoQ. Given a list of integers, find the first non-repeating integer using the Stream ...read more

Popular interview questions of Associate Software Engineer

An Associate Software Engineer was asked 9mo agoQ1. Write a program to check whether the parentheses are properly balanced, meaning ...read more
An Associate Software Engineer was asked 9mo agoQ2. Given a Map of fruits with their price as a key-value pair respectively, find th...read more
An Associate Software Engineer was asked 9mo agoQ3. Given a list of integers, find the first non-repeating integer using the Stream ...read more
ZeMoSo Technologies Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits