Suppose we have two strings s and t. We have to check whether t can be formed using characters of s or not.
So, if the input is like s = "owleh" t = "hello", then the output will be True.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
from collections import defaultdict def solve(s, t): freq = defaultdict(int) for i in range(len(s)): freq[s[i]] += 1 for i in range(len(t)): if freq[t[i]] == 0: return False freq[t[i]] -= 1 return True s = "owhtlleh" t = "hello" print(solve(s, t))