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
4mo

Calculate the minimum number of left shift operations needed to achieve the longest common prefix between two strings.

  • Apply left shift operations to string B to find the longest common prefix with str...read more

Sonu kumar
9mo
A= abcd B=cdab B ko 2 bar left shift karne ke bad B= abcd
Help your peers!
Select
Add answer anonymously...

Bharti Airtel Software Developer interview questions & answers

A Software Developer was asked 2d agoQ. What does a software developer do?
A Software Developer was asked 2d agoQ. What is this engineering
A Software Developer was asked Q. Write an SQL query to find the second highest salary.

Popular interview questions of Software Developer

A Software Developer was asked 2d agoQ1. What is this engineering
A Software Developer was asked Q2. Write an SQL query to find the second highest salary.
A Software Developer was asked Q3. Stack using Two Queues Problem Statement Develop a Stack Data Structure to store...read more
Bharti Airtel Software Developer 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