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.
Sonu kumar
4mo
A= abcd B=cdab B ko 2 bar left shift karne ke bad B= abcd
Help your peers!
Add answer anonymously...
Bharti Airtel Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter