BackmediumStrings Infosys

Galactic Transmission Correction Solution

Problem Statement

Given a transmission string and a subsequence of that string representing corrupted data, find the minimum number of operations required to correct the subsequence in-place, where an operation is a swap of two adjacent characters in the string.

Example 1
Input
transmission = 'abcde', subsequence = 'cde'
Output
2

Explanation: Step-by-step: 1. Find the position of 'c' in the transmission string, which is 2. 2. Find the position of 'c' in the subsequence, which is 0. 3. Swap 'c' with 'b' in the transmission string. 4. Find the position of 'd' in the transmission string, which is 3. 5. Find the position of 'd' in the subsequence, which is 1. 6. Swap 'd' with 'e' in the transmission string. The corrected subsequence is 'cde' in 'abcde' with 2 swaps.

Example 2
Input
transmission = 'abcde', subsequence = 'ceba'
Output
3

Explanation: Step-by-step: 1. Find the position of 'c' in the transmission string, which is 2. 2. Find the position of 'c' in the subsequence, which is 0. 3. Swap 'c' with 'b' in the transmission string. 4. Find the position of 'e' in the transmission string, which is 4. 5. Find the position of 'e' in the subsequence, which is 1. 6. Swap 'e' with 'a' in the transmission string. 7. Find the position of 'b' in the transmission string, which is 1. 8. Find the position of 'b' in the subsequence, which is 3. 9. Swap 'b' with 'c' in the transmission string. The corrected subsequence is 'ceba' in 'abcde' with 3 swaps.

Constraints

  • 1 <= length of transmission string <= 1000
  • 1 <= length of subsequence <= 100
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free