Skip to content

Commit

Permalink
fix(ransom-note): does not cover all test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
esau-morais committed May 15, 2024
1 parent c72cb9f commit b88c750
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/solutions/ransom-note.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
// O(n*m)
// O(n+m)
export function canConstruct(ransomNote: string, magazine: string): boolean {
const needleIdx = magazine.indexOf(ransomNote);
return needleIdx !== -1;
const magazineMap = new Map<string, number>();

for (const char of magazine) {
if (magazineMap.has(char)) {
magazineMap.set(char, magazineMap.get(char)! + 1);
} else {
magazineMap.set(char, 1);
}
}

for (const char of ransomNote) {
if (magazineMap.get(char)) {
magazineMap.set(char, magazineMap.get(char)! - 1);
} else {
return false;
}
}

return true;
}

0 comments on commit b88c750

Please sign in to comment.