Hook
More breakout videos from this creator.
Can You Solve This Facebook Interview Problem? All right today, we're gonna cover Ransom Note. Given two strings, Ransom Note and magazine, return true if Ransom Note can be constructed using the letters from magazine and false otherwise. Each letter in magazine can only be used once in Ransom Note. Example 1: Input: ransomNote = "a", magazine = "b" Output: false. Example 2: Input: ransomNote = "aa", magazine = "ab" Output: false. Example 3: Input: ransomNote = "aa", magazine = "aab" Output: true. Ransom note is 'aa', magazine is 'aab'. Output should be true because there's 2 A's inside our magazine, 2 A's inside our ransom note. All right, let's solve this question. We already know the counts of each letter. So let's make a hash map. Need = defaultdict(int). This hash map stores our letter and its count. Then for each letter we find inside the ransom note, let's increment the count. for char in ransomNote: need[char] += 1. Then for each letter we find inside the magazine, we can subtract that amount from what we need. for char in magazine: need[char] -= 1. All right, now we know the values of our hash map are the counts of the letters. If any of those values are above 0, then we have to return false. return not any([need_count > 0 for need_count in need.values()]). All right, let's see if this works. All right, there we go. We beat 91% of Python submissions. Comment down below if you would have passed this interview and follow for more!