Intuition

First count the ocurrence of each character in letters. After this we will check in each word that the frequency of each chracters in that word should not exceed the letter.

Implementation

  1. Take two vector v1 & v2 of size 26 and initialze them with 0.
  2. Then by using foreach loop count the frequency of each character in letter.
  3. After this, Compare the frequency of each character in word with letter.

Time Complexity

O(M∗N) : where M is the length of the longest word in words and N is the length of words

Space Complexity

O(1) : We are only taking constant space.

Code