Click to view the answer
Traverses `ransomNote` and subtracts one from the number corresponding to the current character (reverse operation). If the number of a character is less than 0, return `false`.
## Steps
1. First count the characters in `magazine`, and store the results in `Map`.
```javascript
charToCount = new Map()
for (character in magazine) {
charToCount[character] += 1
}
```
2. Then, traverse `ransomNote` and perform reverse operations on the data in `Map`. If the count of a character is less than 0, return `false`.
```javascript
charToCount = new Map()
for (character in magazine) {
charToCount[character] += 1
}
for (character in ransomNote) {
charToCount[character] -= 1
if (charToCount[character] < 0) {
return false
}
}
return true
```
## Complexity
- Time complexity: `O(N)`.
- Space complexity: `O(N)`.
## Java
```java
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
var charToCount = new HashMap