The problem asks us to find which how many times can the target string be formed from the source string. This is clearly a frequency problem. We keep track of frequency of all characters in a map for both strings. Then the minimum number of characters of string present in string is the answer. This is because if any character falls short, we can’t make a complete target string: thus won’t be considered for a valid answer.
Code:
Java
Big O Analysis
-
Runtime
The runtime complexity here is
O(N)
as there are no nested loops and we have visited the characters of source and target strings once in a loop. -
Memory
The memory usage is
O(26)
since we have arrays of size 26: so, constant space!
— A