ransom-note
Problem
Problem Description
Solution
This problem is to check whether magazine contains all char in ransomNote,
if magazine contains all characters in ransomNote, then true
otherwise any character in ransomNote but not in magazine, return false.
Using Map to keep char and frequency as pair in map.
Iterate magazine characters, add characters and frequency into map
Iterate ransomNote, check each character, if not exist in map or current frequency in map <= 0, then return false. otherwise continue.
For each visit, in map, put frequency - 1 back into map, continue
when done iterate ransomNote, all chars in map (in magazine)
For example:
Complexity Analysis
Time Complexity: O(N)
Space Complexity: O(N)
N - Max (ransom note length, magazine length)
Code
Last updated