Prepin
Log in

Given two strings (Word 1 and Word 2) and a list of acceptable words in a dictionary, determine the least number of operations needed to transform Word 1 into Word 2.

Approach / Explanation

This question requires you to create an algorithm that calculates the minimum number of operations (insertions, deletions, and substitutions) needed to convert one string into another, ensuring that each operation results in a valid word from a specified dictionary. This type of problem is often known as finding the shortest edit distance, specifically a constrained version of the Levenshtein distance where each intermediate step must yield a valid word. It is crucial to recognize that this problem has two main components: you need to modify the string, and every modification must create a valid word from the given dictionary. This issue can be tackled using either dynamic programming or a breadth-first search algorithm with a queue. When working on this question, consider the following important points: Determine if the operations are case-sensitive. Consider how to optimize your solution to avoid revisiting words that have already been processed.

Suggested Answer

In Python, the Breadth-First Search (BFS) approach can be utilized to determine the minimum number of operations needed. def min_operation ( word1 , word2 , words ) : wordSet = set ( words ) queue = collections . deque ( [ ( word1 , 1 ) ] ) while queue : w , op = queue . popleft ( ) if w == word2 : return op for i in range ( len ( w ) ) : for c in 'abcdefghijklmnopqrstuvwxyz' : new_word = w [ : i ] + c + w [ i + 1 : ] if new_word in wordSet and new_word != w : queue . append ( ( new_word , op + 1 ) ) wordSet . remove ( new_word ) return 0 This function operates by examining each possible single-character alteration for every position in the current word and verifying if the resulting new word exists in our wordSet (dictionary). If it does, that new word is added to the queue to be processed with one additional operation compared to the current word.

Alternative Answer

Below is a more comprehensive Python implementation that employs an optimization strategy to efficiently create valid word transformations: def min_operation ( word1 , word2 , words ) : len1 = len ( word1 ) len2 = len ( word2 ) wordSet = set ( words ) queue = collections . deque ( [ ( word1 , 1 ) ] ) if word1 not in wordSet or word2 not in wordSet or len1 != len2 : return 0 while queue : cur_word , op = queue . popleft ( ) if cur_word == word2 : return op for i in range ( len1 ) : for j in range ( ord ( 'a' ) , ord ( 'z' ) + 1 ) : if chr ( j ) == cur_word [ i ] : continue new_word = cur_word [ : i ] + chr ( j ) + cur_word [ i + 1 : ] if new_word in wordSet : wordSet . remove ( new_word ) queue . append ( ( new_word , op + 1 ) ) return 0 This implementation optimizes performance by avoiding unnecessary operations, continuing the loop if the current character matches the one that is being replaced.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Airbnb

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.