Skip to content

Commit

Permalink
Time: 10 ms (59.23%), Space: 45.1 MB (30.54%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit-S-Sahu committed Oct 23, 2024
1 parent db8caee commit 6e25a4c
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions 0383-ransom-note/0383-ransom-note.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {

HashMap<Character, Integer> map = new HashMap<>();

for (int i = 0; i < magazine.length(); i++) {
char c = magazine.charAt(i);

if (!map.containsKey(c)) {
map.put(c, 1);
} else {
map.put(c, map.get(c) + 1);
}
for (Character ch : magazine.toCharArray()) {
map.put(ch, map.getOrDefault(ch, 0) + 1);
}

for (int i = 0; i < ransomNote.length(); i++) {
char c = ransomNote.charAt(i);

if (map.containsKey(c) && map.get(c) > 0) {
map.put(c, map.get(c) - 1);
} else {
return false;
}

for (Character ch : ransomNote.toCharArray()) {
if (!map.containsKey(ch) || map.get(ch) <= 0) return false;
map.put(ch, map.get(ch) - 1);
}

return true;
}
}

0 comments on commit 6e25a4c

Please sign in to comment.