spiralgo / algorithms

We voluntarily solve algorithm problems on this repo.
24 stars 7 forks source link

1153. String Transforms Into Another String #388

Closed ErdemT09 closed 3 years ago

ErdemT09 commented 3 years ago

https://leetcode.com/problems/string-transforms-into-another-string/

Resolves: #239

Algorithm:

A string is not convertible to another string under these 2 conditions:

  1. A character transforms into more than one character: "aaa" -> "hgf" is not possible.
  2. The second string contains all possible letters: When both strings contain same letters as in: "abc" -> "bca", we can convert individual letters without making them same by temporarily converting them to another letters like this: "abc" -> "dbc" -> "dba" -> "dca" -> "bca" As long as we have a letter not present in the destination string, we can do such temporary conversions.

We only have to check for these.