Suppose we have two strings s and t. We can delete t from s any number of times. And t appears only once at a time. We have to check whether s can become empty by removing t as many times as required.
So, if the input is like s = "pipipinnn" t = "pin", then the output will be True as we can remove "pin" from "pipipinnn", then we will get "pipinn", again remove "pin" to get string "pin", then remove it to make it empty.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
def solve(s, t): while len(s) > 0: position = s.find(t) if position == -1: break s = s.replace(t, "", 1) return len(s) == 0 s = "pipipinnn" t = "pin" print(solve(s, t))