Minimum Operations to Transform Strings

You are provided with two strings initial and final. Each string consists of characters 'a', 'b', and one empty slot represented by '_'. Your task is to transform the initial string into the final string using the minimum number of operations.

Allowed Operations:

1. Swap the empty character (_) with any adjacent character.
2. Swap the empty character with the next to adjacent character only if the adjacent and next to adjacent characters are different.

Input:

The first line contains an integer 'T', the number of test cases.
The following 2*T lines describe each test case:
- First line: string 'initial'.
- Second line: string 'final'.

Output:

For each test case, output a single integer that represents the minimum number of operations required to transform the initial string to the final string.

Example:

Input:
2
aba_b
ab_ab
ab_a
a_ab
Output:
1
3

Constraints:

  • 1 <= T <= 25
  • 1 <= length(initial), length(final) <= 10
  • Time limit: 1 sec

Note:

1. You should not print anything; just implement the function to return the result.
2. Both strings have the same length.
AnswerBot
12d

The task is to transform one string into another by swapping characters with an empty slot, using the minimum number of operations.

  • Iterate through the strings character by character and count the numb...read more

Help your peers!
Add answer anonymously...
Daffodil Software 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