Minimum Operation Needed to Convert to the Given String

You are given two strings str1 and str2. Determine the minimum number of operations required to transform str1 into str2.

Explanation:

An operation is defined as taking a character from an index in str1 and moving it to the end of str1. This counts as one operation.

Note:

No operations can be performed on str2.

Input:

The first line of input contains an integer 'T' denoting the number of test cases. 
The first line of each test case contains a string for str1.
The second line of each test case contains a string for str2.

Output:

For each test case, print the minimum number of operations needed to transform str1 into str2. Print -1 if the transformation is not possible. Output each result on a separate line.

Example:

Input:
T = 2 
str1 = "abcd"
str2 = "dabc"
str1 = "abc"
str2 = "acb"
Output:
1 
-1
Explanation:

In the first test case, shifting "a" to the end once will convert "abcd" to "dabc".

In the second test case, no sequence of operations on "abc" can result in "acb".

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 105
  • 1 ≤ M ≤ 105
  • Strings consist only of lowercase and uppercase English letters.
AnswerBot
14d

Determine the minimum number of operations needed to transform one string into another by moving characters to the end.

  • Iterate through each character in str1 and check if it matches the first characte...read more

Help your peers!
Add answer anonymously...
Microsoft Corporation SDE-2 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