Skip to content

Commit ba6683e

Browse files
committed
solve problem Ransom Note
1 parent 30ef111 commit ba6683e

File tree

5 files changed

+72
-0
lines changed

5 files changed

+72
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ All solutions will be accepted!
4545
|145|[Binary Tree Postorder Traversal](https://leetcode-cn.com/problems/binary-tree-postorder-traversal/description/)|[java/py/js](./algorithms/BinaryTreePostorderTraversal)|Hard|
4646
|231|[Power Of Two](https://leetcode-cn.com/problems/power-of-two/description/)|[java/py/js](./algorithms/PowerOfTwo)|Easy|
4747
|38|[Count And Say](https://leetcode-cn.com/problems/count-and-say/description/)|[java/py/js](./algorithms/CountAndSay)|Easy|
48+
|383|[Ransom Note](https://leetcode-cn.com/problems/ransom-note/description/)|[java/py/js](./algorithms/RansomNote)|Easy|
4849

4950
# Database
5051
|#|Title|Solution|Difficulty|

algorithms/RansomNote/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ransom Note
2+
This problem is easy to solve by hashmap

algorithms/RansomNote/Solution.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public boolean canConstruct(String ransomNote, String magazine) {
3+
Map<Character, Integer> characterMap = new HashMap<Character, Integer>();
4+
for (char c : magazine.toCharArray()) {
5+
if (characterMap.get(c) == null) {
6+
characterMap.put(c, 1);
7+
} else {
8+
characterMap.put(c, characterMap.get(c) + 1);
9+
}
10+
}
11+
12+
for (char c: ransomNote.toCharArray()) {
13+
if (characterMap.get(c) == null || characterMap.get(c) == 0) {
14+
return false;
15+
} else {
16+
characterMap.put(c, characterMap.get(c) - 1);
17+
}
18+
}
19+
return true;
20+
}
21+
}

algorithms/RansomNote/solution.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {string} ransomNote
3+
* @param {string} magazine
4+
* @return {boolean}
5+
*/
6+
var canConstruct = function(ransomNote, magazine) {
7+
let characterMap = {}
8+
for (let i = 0; i < magazine.length; i++) {
9+
let c = magazine[i]
10+
if (characterMap[c] !== undefined) {
11+
characterMap[c] += 1
12+
} else {
13+
characterMap[c] = 1
14+
}
15+
}
16+
17+
for (let i = 0; i < ransomNote.length; i++) {
18+
let c = ransomNote[i]
19+
if (characterMap[c] === undefined || characterMap[c] === 0) {
20+
return false
21+
} else {
22+
characterMap[c] -= 1
23+
}
24+
}
25+
26+
return true
27+
};

algorithms/RansomNote/solution.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def canConstruct(self, ransomNote, magazine):
3+
"""
4+
:type ransomNote: str
5+
:type magazine: str
6+
:rtype: bool
7+
"""
8+
character_map = {}
9+
for c in magazine:
10+
if character_map.get(c) != None:
11+
character_map[c] += 1
12+
else:
13+
character_map[c] = 1
14+
15+
for c in ransomNote:
16+
if character_map.get(c) == None or character_map.get(c) == 0:
17+
return False
18+
else:
19+
character_map[c] -= 1
20+
return True
21+

0 commit comments

Comments
 (0)