310. Ransom Note

Easy
Frequency Consumption
Hashing

Problem

Given two strings ransomNote and magazine, return true if ransomNote can be constructed using characters from magazine. Each character in magazine may be used at most once.

Examples

Example 1

Input: "a", "b"
Output: false

Example 2

Input: "aa", "ab"
Output: false

Example 3

Input: "aa", "aab"
Output: true
Constraints

1 <= ransomNote.length, magazine.length <= 100000

Both strings consist of lowercase English letters.

Hints

?? Count the available characters in magazine.

?? Consume one count for every character required by ransomNote.

?? If any required count becomes unavailable, return false.

Expected Complexity
Time: O(n + m)
Space: O(1)
Follow-up

Can you implement the solution using a fixed-size array rather than a hash map?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
"a", "b"
Expected
false