Minimum Steps to Convert String with Operations - Problem
Transform One String Into Another with Minimal Operations

You're given two strings word1 and word2 of equal length, and your goal is to transform word1 into word2 using the minimum number of operations.

Here's how it works: You can partition word1 into one or more contiguous substrings. For each substring, you have three powerful operations at your disposal:

๐Ÿ”„ Replace: Change any single character to any other lowercase letter
๐Ÿ”€ Swap: Exchange any two characters within the substring
โ†ฉ๏ธ Reverse: Flip the entire substring backwards

Important constraint: Each character can participate in at most one operation of each type. So a character at position i can be involved in one replace, one swap, AND one reverse operation.

Your mission: Find the minimum total operations needed across all substrings to transform word1 into word2.

For example: "abc" โ†’ "bac" can be done in just 1 operation by swapping 'a' and 'b' in the entire string.

Input & Output

example_1.py โ€” Basic Swap
$ Input: word1 = "abc", word2 = "bac"
โ€บ Output: 1
๐Ÿ’ก Note: We can transform 'abc' to 'bac' with a single swap operation: swap characters at positions 0 and 1 ('a' and 'b'). The entire string can be treated as one partition.
example_2.py โ€” Reverse Operation
$ Input: word1 = "abcd", word2 = "dcba"
โ€บ Output: 1
๐Ÿ’ก Note: The target string 'dcba' is exactly the reverse of 'abcd'. We can achieve this with a single reverse operation on the entire string.
example_3.py โ€” Multiple Operations
$ Input: word1 = "abxy", word2 = "xyba"
โ€บ Output: 2
๐Ÿ’ก Note: Optimal approach: partition into ['ab', 'xy']. Transform 'ab'โ†’'xy' (2 replacements would be 2 ops, but we can reverse 'xy'โ†’'yx' then need 1 replacement) and 'xy'โ†’'ba' (2 replacements). Better: partition as ['abxy'] and reverse to get 'yxba', then 2 swaps. Actually optimal is 2 operations total with right partitioning.

Constraints

  • 1 โ‰ค word1.length, word2.length โ‰ค 100
  • word1.length == word2.length
  • word1 and word2 consist of lowercase English letters only
  • Each character can be used in at most one operation of each type

Visualization

Tap to expand
String Transformation WorkshopInput: "abc" โ†’ "bac"abcโ†’bac๐Ÿช„ Available Tools:๐Ÿชž MirrorReverse substringCost: 1 operation๐Ÿ”„ Swap SpellExchange 2 charsCost: 1 operationโœจ Magic WandReplace charCost: 1 operation๐ŸŽฏ Optimal Strategy:Use Swap Spell on "abc"Swap positions 0โ†”1: "abc" โ†’ "bac"โœ“ 1 Operation!๐Ÿ’ก Key Insight: Always check if beneficial swaps exist before using expensive replacements!
Understanding the Visualization
1
Partition the String
Divide word1 into contiguous sections that can be transformed efficiently
2
Apply Mirror Test
For each section, check if reversing it gets closer to the target
3
Optimize with Swaps
Use beneficial swaps to fix multiple positions with fewer operations
4
Replace Remainder
Use replacements only for characters that can't be fixed by swapping
Key Takeaway
๐ŸŽฏ Key Insight: The optimal approach combines dynamic programming for partitioning with greedy operation selection - always prefer swaps and reverses over replacements when possible!
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 22 Apple 18
27.4K Views
Medium-High Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen