Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if a string can be transformed to another string when only insert/remove/replace operations are allowed

I must write a function that takes two words (strings) as arguments, and determines if the first word can be transformed into the second word using only one first-order transformation.

  • First-order transformations alter only one letter in a word

  • The allowed transformations are: insert, remove and replace

    • insert = insert a letter at any position in the word
    • remove = delete a letter from any position in the word
    • replace = replace a letter with another one

Any suggestions? Any Java examples would be great!

like image 716
Matey Avatar asked Nov 30 '25 06:11

Matey


1 Answers

Think: If you're only allowed a single transformation, then the difference in length between the "before" and "after" words should give you a very strong hint as to which of those three transformations has any chance of being successful. By the same token, you can tell at a glance which transformations will be simply impossible.

Once you've decided on which transformation, the rest of the problem becomes a job for Brute Force Man and his sidekick, Looping Lady.

like image 141
Carl Smotricz Avatar answered Dec 02 '25 22:12

Carl Smotricz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!