Suppose we have two strings s and t, we have to check whether we can generate t by swapping the character of the s.
So, if the input is like s = "worldlloeh" t = "helloworld", then the output will be True as we can swap characters from "worldlloeh" to make "helloworld".
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): s_len = len(s) t_len = len(t) if (s_len != t_len): return False freq = defaultdict(int) for char in s : freq[char] += 1 for i in range(t_len) : freq[t[i]] -= 1 if freq[t[i]] < 0: return False return True s = "worldlloeh" t = "helloworld" print(solve(s, t))