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

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!
Select
Add answer anonymously...

Daffodil Software Software Developer interview questions & answers

A Software Developer was asked Q. What are your qualifications?
A Software Developer was asked Q. Given an array of numbers, find the largest prime number in the array using the ...read more
A Software Developer was asked Q. Write a program to print the pattern.

Popular interview questions of Software Developer

A Software Developer was asked 5mo agoQ1. What are your hobbies?
A Software Developer was asked Q2. What are your qualifications?
A Software Developer was asked Q3. Given an array of numbers, find the largest prime number in the array using the ...read more
Daffodil Software 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