AlgoMaster Logo

Ransom Note

Ashish

Ashish Pratap Singh

easy

Problem Description

Solve it on LeetCode

Approaches

1. Frequency Counting Using Arrays

Intuition:

The problem is asking whether we can construct the ransomNote string using the characters from the magazine string. We can think of this as checking if there are enough characters available in magazine to cover each character requirement in ransomNote.

The basic idea is to count the frequency of each character in both ransomNote and magazine. Then, check if for every character in ransomNote, the count of that character in magazine is greater than or equal to its count in ransomNote.

Code:

We can use an integer array of size 26 to count the frequency of characters since the problem guarantees lowercase English letters only.

2. Frequency Counting Using HashMap

Intuition:

Similar to the array-based approach, but using a HashMap allows the solution to easily be extended to encompass a broader set of characters if constraints were to change.

Code:

We will use a HashMap to store frequencies of characters in magazine and then check each character in ransomNote against this frequency map.