1055. Shortest Way to Form String

Question

CleanShot 2024-09-07 at 17.27.18@2x.png

Code

class Solution:
    def shortestWay(self, source: str, target: str) -> int:
        
        tp = 0

        count = 0

        while tp < len(target):

            newTp = tp

            for i in range(len(source)):
                if newTp >= len(target):
                    break

                if source[i] == target[newTp]:
                    newTp += 1


            if newTp == tp:
                return -1

            tp = newTp
            count += 1

        return count