{"id":"ransom-note","title":"Ransom Note","difficulty":"easy","patterns":["hash-map"],"primaryConcept":"hash-map-grouping","concepts":["hash-map-grouping"],"problemForm":"intro","companies":["Amazon","Meta","Google"],"category":"hash-tables","executionStyle":"solution-method","compareMode":"exact","syntheticTestConfig":{"inputShape":"two-strings","primaryParam":"ransomNote","secondaryParam":"magazine","sizeMetric":"total-length","sizes":[8,16,32,64]},"description":"Given two strings `ransomNote` and `magazine`, return `true` if `ransomNote` can be constructed using letters from `magazine`. Each letter in `magazine` can be used at most once.","examples":[{"input":"ransomNote = \"a\", magazine = \"b\"","output":"false","explanation":"The magazine does not contain the letter a."},{"input":"ransomNote = \"aa\", magazine = \"aab\"","output":"true","explanation":"The magazine has two copies of a, so both letters can be used."},{"input":"ransomNote = \"aab\", magazine = \"baa\"","output":"true","explanation":"Order does not matter; only available character counts matter."}],"constraints":[{"id":"1-len-ransomnote-len-magazine-10-5","text":"1 <= len(ransomNote), len(magazine) <= 10^5","check":[{"kind":"sizeRange","target":"ransomNote","measure":"stringLength","min":1,"max":100000},{"kind":"sizeRange","target":"magazine","measure":"stringLength","min":1,"max":100000}]},{"id":"ransomnote-and-magazine-consist-of-lowercase-english-letters","text":"ransomNote and magazine consist of lowercase English letters.","check":[{"kind":"domain","target":"ransomNote","selector":"stringChars","allowed":["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]},{"kind":"domain","target":"magazine","selector":"stringChars","allowed":["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]}]},{"id":"each-magazine-character-can-be-consumed-at-most-once","text":"Each magazine character can be consumed at most once."}],"starterCode":"class Solution:\n    def canConstruct(self, ransomNote: str, magazine: str) -> bool:\n        # Your code here\n        pass","functionName":"canConstruct","testCases":[{"id":"1","input":{"ransomNote":"a","magazine":"b"},"expected":false,"description":"Missing required letter."},{"id":"2","input":{"ransomNote":"aa","magazine":"ab"},"expected":false,"description":"One copy is not enough for two required letters."},{"id":"3","input":{"ransomNote":"aa","magazine":"aab"},"expected":true,"description":"Enough copies are available."},{"id":"4","input":{"ransomNote":"abc","magazine":"cbad"},"expected":true,"description":"Order is irrelevant."},{"id":"5","input":{"ransomNote":"aabbcc","magazine":"abcabc"},"expected":true,"description":"Every duplicated character is matched exactly."},{"id":"h1","input":{"ransomNote":"leetcode","magazine":"coleetde"},"expected":true,"description":"All letters are present in a different order."},{"id":"h2","input":{"ransomNote":"hello","magazine":"helo"},"expected":false,"description":"The second l is missing."},{"id":"h3","input":{"ransomNote":"zzzz","magazine":"zzzzzz"},"expected":true,"description":"Extra magazine letters are allowed."},{"id":"h4","input":{"ransomNote":"abcabc","magazine":"abccba"},"expected":true,"description":"Counts match even when the order is reversed."},{"id":"h5","input":{"ransomNote":"abcabc","magazine":"aabbc"},"expected":false,"description":"One c is missing."}],"hints":[{"level":1,"content":"The order of letters does not matter, but duplicate counts do.","revealPattern":false},{"level":2,"content":"Count how many times each magazine character appears, then consume those counts while scanning the ransom note.","revealPattern":true},{"level":3,"content":"If a needed character has count `0`, return `false` immediately; otherwise decrement its count.","revealPattern":true}],"guidedFlow":{"version":2,"progressRevision":"ransom-note-2026-11-20-deed-from-ceded","approachSelection":{"contract":"approach-selection-v1","question":"Which way of checking the note should the code use?","decisionPrompt":"Compare how many letters each one reads when both strings are long.","approaches":[{"id":"rescan-magazine","name":"Search the magazine for each letter","summary":"For each letter of the note, read the magazine from the front until an uncut copy turns up, then mark it used.","decisionReason":"Every letter of the note starts the magazine over, so two strings of 1,000 e's cost 500,500 reads.","complexity":{"time":"O(n * m)","space":"O(m)"},"tradeoffs":["Needs a used mark for every position in the magazine."],"isOptimal":false},{"id":"count-then-spend","name":"Count the magazine, then spend the counts","summary":"Read the magazine once into a count per letter, then have each letter of the note take one copy from its count.","decisionReason":"Each string is read once, so the same two strings cost 2,000 reads.","complexity":{"time":"O(n + m)","space":"O(1)"},"tradeoffs":["Keeps one count per distinct letter, 26 at most."],"isOptimal":true}]},"codingCheckpoints":[{"id":"init-frequency-map","title":"Create the counts","description":"Start an empty map that will hold one count per letter.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"keyed-state","fact":"initializes_keyed_state","scope":"init-frequency-map","readMode":"keyed-state-value-query-v1"}]},"commonMistakes":["Using a set, which records that a letter appears and loses how many copies there are."],"hint":"It starts empty, and the magazine fills it in.","semanticSpec":{"intent":"Initialize keyed count state before any letter is spent.","doneWhen":"A map-like count state exists before the magazine is tallied.","shouldFailWhen":"This step should fail if no keyed state is initialized.","roles":["count map"],"mustProve":["The solution initializes keyed state."],"mustNotAccept":["A membership-only set representation."],"proofSnippets":[{"id":"py-ransom-init-frequency-map","language":"python","code":"class Solution:\n    def canConstruct(self, ransomNote: str, magazine: str) -> bool:\n        counts = {}"}]}},{"id":"count-magazine","title":"Count every magazine letter","description":"For each letter of the magazine, raise that letter's count by 1.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"keyed-state","fact":"counts_items_by_key","scope":"count-frequencies","readMode":"keyed-state-value-query-v1"}]},"commonMistakes":["Writing 1 for every letter, so a repeated letter never gets past 1.","Counting the letters of the note here. The counts describe what the magazine can give."],"hint":"A letter with no entry yet has been seen 0 times, so its first copy makes the count 1.","semanticSpec":{"intent":"Accumulate letter multiplicity in keyed state.","doneWhen":"Each magazine letter raises its own count bucket.","shouldFailWhen":"This step should fail if repeats are not accumulated.","roles":["current letter","count map"],"mustProve":["The keyed state counts items by key."],"mustNotAccept":["A write that discards earlier counts."],"proofSnippets":[{"id":"py-ransom-count-magazine","language":"python","code":"class Solution:\n    def canConstruct(self, ransomNote: str, magazine: str) -> bool:\n        counts = {}\n        for char in magazine:\n            counts[char] = counts.get(char, 0) + 1"}]}},{"id":"scan-ransom-note","title":"Go through the note letter by letter","description":"Visit each letter of the note. Every one of them has to take a copy.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"array-scan","fact":"scans_input_sequence","scope":"loop-through-demands","readMode":"array-scan-value-query-v1"}]},"commonMistakes":["Looping over the magazine a second time."],"hint":"This loop comes after the counting loop has finished, and it reads the other string.","semanticSpec":{"intent":"Scan each demanded letter from the note.","doneWhen":"The solution loops across the demanded letters after the count map is built.","shouldFailWhen":"This step should fail if only the counting input is scanned.","roles":["ransom note","current letter"],"mustProve":["The traversal reads a demanded-letter input sequence separate from the count-building scan."],"mustNotAccept":["A fixed single-letter check."],"proofSnippets":[{"id":"py-ransom-scan-ransom-note","language":"python","code":"class Solution:\n    def canConstruct(self, ransomNote: str, magazine: str) -> bool:\n        counts = {}\n        for char in magazine:\n            counts[char] = counts.get(char, 0) + 1\n        for char in ransomNote:\n            pass"}]}},{"id":"check-available-count","title":"Refuse a letter with no copies left","description":"If the current letter's count is 0, return false right away.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"keyed-state","fact":"validates_keyed_lookup_value","scope":"check-available-count","readMode":"keyed-state-value-query-v1"}]},"commonMistakes":["Checking only that the letter has an entry. An entry can sit at 0 once its copies are used."],"hint":"Read the count before lowering it. A letter with no entry counts as 0.","semanticSpec":{"intent":"Validate the remaining count before spending a copy.","doneWhen":"The solution compares the remaining count against an exhausted value.","shouldFailWhen":"This step should fail if only key membership is checked.","roles":["current letter","remaining count"],"mustProve":["The solution validates the keyed lookup value."],"mustNotAccept":["Membership-only checks that ignore exhausted counts."],"proofSnippets":[{"id":"py-ransom-check-available-count","language":"python","code":"class Solution:\n    def canConstruct(self, ransomNote: str, magazine: str) -> bool:\n        counts = {}\n        for char in magazine:\n            counts[char] = counts.get(char, 0) + 1\n        for char in ransomNote:\n            if counts.get(char, 0) == 0:\n                return False"}]}},{"id":"consume-count","title":"Take one copy","description":"When the letter is allowed, lower its count by 1.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"keyed-state","fact":"updates_keyed_counter_after_lookup_consumption","scope":"consume-count","readMode":"keyed-state-value-query-v1"}]},"commonMistakes":["Checking the count and never lowering it, which lets one printed letter be used again and again."],"hint":"The count has to show one fewer copy before the next letter asks.","semanticSpec":{"intent":"Lower the keyed count after a lookup confirms availability.","doneWhen":"The same count bucket is lowered after the availability check passes.","shouldFailWhen":"This step should fail if counts are read but never spent.","roles":["count map","current letter"],"mustProve":["The keyed counter is updated after lookup consumption."],"mustNotAccept":["A solution that never lowers counts."],"proofSnippets":[{"id":"py-ransom-consume-count","language":"python","code":"class Solution:\n    def canConstruct(self, ransomNote: str, magazine: str) -> bool:\n        counts = {}\n        for char in magazine:\n            counts[char] = counts.get(char, 0) + 1\n        for char in ransomNote:\n            if counts.get(char, 0) == 0:\n                return False\n            counts[char] -= 1"}]}},{"id":"return-result","title":"Return true when the note is finished","description":"If every letter of the note took a copy, return true after the loop.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"array-scan","fact":"returns_scan_decision","scope":"return-decision","readMode":"array-scan-value-query-v1"}]},"commonMistakes":["Returning true inside the loop, after the first letter that passes."],"hint":"Spare magazine letters and counts that reached 0 do not change this answer.","semanticSpec":{"intent":"Return a scan-driven yes/no verdict.","doneWhen":"The function returns a boolean tied to the note scan.","shouldFailWhen":"This step should fail if the scan never drives the decision.","roles":["scan decision","return value"],"mustProve":["The solution returns a scan decision."],"mustNotAccept":["Returning unrelated state."],"proofSnippets":[{"id":"py-ransom-return-result","language":"python","code":"class Solution:\n    def canConstruct(self, ransomNote: str, magazine: str) -> bool:\n        counts = {}\n        for char in magazine:\n            counts[char] = counts.get(char, 0) + 1\n        for char in ransomNote:\n            if counts.get(char, 0) == 0:\n                return False\n            counts[char] -= 1\n        return True"}]}}],"slides":[{"id":"opener","kind":"info","title":"Cut a note out of a magazine","workspacePanel":"problem","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"input","state":{"arrays":[{"name":"ransomNote","values":["d","e","e","d"]},{"name":"magazine","values":["c","e","d","e","d"]}]}}]},"body":["You want to write a note by cutting letters out of a magazine. Each printed letter can be cut out once.","The note and the magazine both arrive as strings of lowercase letters. Return true if the magazine has enough letters to write the whole note, and false if it does not.","In this input the note is \"deed\" and the magazine is \"ceded\"."]},{"id":"first-example","kind":"case-probe","title":"Can \"deed\" be written with letters cut from \"ceded\"?","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"first-example-input","state":{"arrays":[{"name":"ransomNote","values":["d","e","e","d"]},{"name":"magazine","values":["c","e","d","e","d"]}]}}]},"options":[{"id":"yes-enough-copies","label":"Yes: the magazine has two d's and two e's, with a c to spare","rationale":"The note uses d twice and e twice, and the magazine prints each of them twice.","isCorrect":true},{"id":"no-wrong-order","label":"No: the magazine never has d, e, e, d next to each other","rationale":"Cut letters can be pasted down in any order, so where they sit in the magazine does not matter.","isCorrect":false},{"id":"no-unused-letter","label":"No: the c in the magazine would go unused","rationale":"Nothing says the magazine has to be used up. Only the note has to be complete.","isCorrect":false}]},{"id":"second-example","kind":"case-probe","title":"Can \"deed\" still be written if the magazine is \"coded\"?","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"second-example-input","state":{"arrays":[{"name":"ransomNote","values":["d","e","e","d"]},{"name":"magazine","values":["c","o","d","e","d"]}]}}]},"options":[{"id":"no-one-e","label":"No: the note needs two e's and the magazine prints one","rationale":"The first e of the note uses the only copy, and the second e has nothing left to cut. Changing one letter, the magazine's first e to an o, turned yes into no.","isCorrect":true},{"id":"yes-both-appear","label":"Yes: d and e both appear in the magazine","rationale":"Appearing once is enough for one use. The note asks for e twice.","isCorrect":false},{"id":"no-spare-letters","label":"No: the magazine has a c and an o the note never uses","rationale":"Spare letters were harmless in \"ceded\" and they are harmless here. The shortage is in the e's.","isCorrect":false}]},{"id":"brute-force","kind":"info","title":"By hand, you would search and cut","workspacePanel":"visualizer","body":["Take the note one letter at a time. For each letter, read the magazine from its first letter until you reach a copy that has not been cut, and cut it out."],"visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"search-first-d","title":"The first d takes 3 reads","summary":"The search passes c and e, then cuts the d.","state":{"arrays":[{"name":"ransomNote","values":["d","e","e","d"],"current":0},{"name":"magazine","values":["c","e","d","e","d"],"current":2}],"reads":3}},{"id":"search-first-e","title":"The first e takes 2 reads","summary":"It starts again from the front, passes c, and cuts the e.","state":{"arrays":[{"name":"ransomNote","values":["d","e","e","d"],"current":1},{"name":"magazine","values":["c","e","d","e","d"],"current":1,"used":[2]}],"reads":5}},{"id":"search-second-e","title":"The second e takes 4 reads","summary":"From the front again, it passes c, the cut e and the cut d before it reaches the other e.","state":{"arrays":[{"name":"ransomNote","values":["d","e","e","d"],"current":2},{"name":"magazine","values":["c","e","d","e","d"],"current":3,"used":[1,2]}],"reads":9}},{"id":"search-second-d","title":"The second d takes 5 reads","summary":"It passes every earlier letter to reach the last d. The note is complete after 14 reads.","state":{"arrays":[{"name":"ransomNote","values":["d","e","e","d"],"current":3},{"name":"magazine","values":["c","e","d","e","d"],"current":4,"used":[1,2,3]}],"reads":14}}]}},{"id":"waste-probe","kind":"case-probe","title":"Suppose the note is 1,000 e's and the magazine is 1,000 e's. About how many letters does the search read in total?","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"search-finished","state":{"arrays":[{"name":"ransomNote","values":["d","e","e","d"],"current":3},{"name":"magazine","values":["c","e","d","e","d"],"current":4,"used":[1,2,3]}],"reads":14}}]},"options":[{"id":"about-half-a-million","label":"About 500,000","rationale":"The first e is found in 1 read, the second in 2 because it passes a cut copy, and the last in 1,000. Adding 1 through 1,000 gives 500,500, and 499,500 of those reads land on copies already cut out.","isCorrect":true},{"id":"about-one-thousand","label":"About 1,000","rationale":"That would be one read for each letter of the note, and only the first e is found in one read. Every later e has to pass the copies cut before it.","isCorrect":false},{"id":"about-two-thousand","label":"About 2,000","rationale":"That would be each string read once, and the magazine is read from the front again for every letter of the note.","isCorrect":false}]},{"id":"pattern-quiz","kind":"case-probe","title":"What could you write down about the magazine, once, so that no letter of the note needs a search?","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"record-input","state":{"arrays":[{"name":"ransomNote","values":["d","e","e","d"]},{"name":"magazine","values":["c","e","d","e","d"]}]}}]},"options":[{"id":"copies-per-letter","label":"How many copies of each letter it has","rationale":"One lookup of a letter's number replaces a walk through the magazine, and where the copies sit never comes up. A map holds this record: it pairs each letter with a number. Call it `counts`.","isCorrect":true},{"id":"which-letters-appear","label":"Which letters appear in it","rationale":"That list says e is there for both \"ceded\" and \"coded\", and those two magazines gave different answers.","isCorrect":false},{"id":"first-copy-position","label":"The position of each letter's first copy","rationale":"The first copy is gone after one use, and the next request for that letter is back to searching.","isCorrect":false}]},{"id":"state-meaning","kind":"dry-run","source":"trace-example","title":"Count the letters of \"ceded\"","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"count-start","state":{"arrays":[{"name":"magazine","values":["c","e","d","e","d"],"current":0}],"seen":{},"labels":{"seen":"counts"}}},{"id":"count-before-repeats","state":{"arrays":[{"name":"magazine","values":["c","e","d","e","d"],"current":3}],"seen":{"c":1,"e":1,"d":1},"seenHighlight":"e","labels":{"seen":"counts"}}},{"id":"count-finished","state":{"arrays":[{"name":"magazine","values":["c","e","d","e","d"],"current":4}],"seen":{"c":1,"e":2,"d":2},"labels":{"seen":"counts"}}}]},"beats":[{"id":"count-first-letter","title":"The first letter is c, and the counts are empty. What do they hold after this letter?","options":[{"id":"c-one","label":"c: 1","rationale":"A letter with no entry has been seen 0 times. This c makes it 1.","isCorrect":true},{"id":"c-zero","label":"c: 0","rationale":"0 would say the magazine has no c to give, and it has this one.","isCorrect":false},{"id":"still-empty","label":"Still empty: a letter is recorded once it repeats","rationale":"Then a letter printed once would never be counted, and no note could use it.","isCorrect":false}]},{"id":"count-repeats","title":"e and d were counted next, at 1 each. The last two letters are a second e and a second d. What do the counts hold after them?","options":[{"id":"repeats-add-one","label":"c: 1, e: 2, d: 2","rationale":"Each repeat adds 1 to the entry that is already there. The counts now hold one c, two e's and two d's, and the magazine is never read again.","isCorrect":true},{"id":"repeats-stay-one","label":"c: 1, e: 1, d: 1","rationale":"Then e would read 1 for \"ceded\" and 1 for \"coded\", and those two magazines have to give different answers.","isCorrect":false},{"id":"repeats-new-entries","label":"c: 1, e: 1, d: 1, e: 1, d: 1","rationale":"A map keeps one entry per key, so a repeat has to change the entry that exists.","isCorrect":false}]}]},{"id":"trace","kind":"dry-run","source":"trace-example","title":"Write \"deed\" from the counts","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"note-first-d","state":{"arrays":[{"name":"ransomNote","values":["d","e","e","d"],"current":0}],"seen":{"c":1,"e":2,"d":2},"seenHighlight":"d","labels":{"seen":"counts"}}},{"id":"note-first-e","state":{"arrays":[{"name":"ransomNote","values":["d","e","e","d"],"current":1}],"seen":{"c":1,"e":2,"d":1},"seenHighlight":"e","labels":{"seen":"counts"}}},{"id":"note-second-e","state":{"arrays":[{"name":"ransomNote","values":["d","e","e","d"],"current":2}],"seen":{"c":1,"e":1,"d":1},"seenHighlight":"e","labels":{"seen":"counts"}}},{"id":"note-second-d","state":{"arrays":[{"name":"ransomNote","values":["d","e","e","d"],"current":3}],"seen":{"c":1,"e":0,"d":1},"seenHighlight":"d","labels":{"seen":"counts"}}},{"id":"note-finished","state":{"arrays":[{"name":"ransomNote","values":["d","e","e","d"]}],"seen":{"c":1,"e":0,"d":0},"labels":{"seen":"counts"},"returns":true}}]},"beats":[{"id":"use-first-d","title":"The note starts with d, and one of the two d's gets used. What should the count for d say afterward?","options":[{"id":"d-drops-to-one","label":"d: 1","rationale":"One d is in the note now, so the magazine has one left to give.","isCorrect":true},{"id":"d-unchanged","label":"d: 2, unchanged","rationale":"Then the counts would still offer two d's after one was used, and the same printed letter could be handed out again.","isCorrect":false},{"id":"d-removed","label":"No entry for d anymore","rationale":"The magazine has a second d, and the note asks for it at the end.","isCorrect":false}]},{"id":"check-first-e","title":"Next is e, and e is at 2. What makes this e allowed?","options":[{"id":"at-least-one","label":"The count is at least 1","rationale":"The count is the number of e's the magazine can still give, and this letter needs one of them.","isCorrect":true},{"id":"matches-note-total","label":"The count matches the two e's in the note","rationale":"The two numbers happen to match in this input. A magazine with three e's would allow this e just the same.","isCorrect":false},{"id":"has-an-entry","label":"e has an entry in the counts, whatever the number","rationale":"An entry stays in the counts after its copies are used up, and then it has nothing to give.","isCorrect":false}]},{"id":"use-last-e","title":"The note asks for e again, and e is at 1. What happens?","options":[{"id":"allowed-drops-to-zero","label":"This e is allowed, and e drops to 0","rationale":"1 means one copy is left, and this letter takes it.","isCorrect":true},{"id":"refused-already-used","label":"This e is refused: e was already used once","rationale":"The magazine printed two e's, and the count says one of them is still there.","isCorrect":false},{"id":"allowed-stays-one","label":"This e is allowed, and e stays at 1","rationale":"Then a third e would be allowed as well, from a magazine that printed two.","isCorrect":false}]},{"id":"return-after-last-letter","title":"The last letter is d, and it takes the last d. What does the function return once the note is finished?","options":[{"id":"true-every-letter-served","label":"true: every letter of the note got a copy","rationale":"No letter was refused, so the magazine could supply the whole note.","isCorrect":true},{"id":"false-counts-at-zero","label":"false: e and d are both at 0","rationale":"0 says those copies were used. It would only matter if another e or d came next, and the note has ended.","isCorrect":false},{"id":"false-c-unused","label":"false: the c was never used","rationale":"This is the input you judged by eye at the start, and its spare c did not stop the answer from being yes.","isCorrect":false}]}]},{"id":"edge-case-intro","kind":"info","title":"The counts wrote \"deed\" from \"ceded\". Next, two other inputs.","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"short-magazine-input","state":{"arrays":[{"name":"ransomNote","values":["d","e","e","d"]},{"name":"magazine","values":["c","o","d","e","d"]}]}}]},"body":["The first is the magazine \"coded\", which has a single e.","The second swaps the two words: the note is \"ceded\" and the magazine is \"deed\"."]},{"id":"not-enough-probe","kind":"case-probe","title":"In the first input, the magazine is \"coded\". The note asks for its second e. What should the function do?","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"second-e-at-zero","state":{"arrays":[{"name":"ransomNote","values":["d","e","e","d"],"current":2},{"name":"magazine","values":["c","o","d","e","d"]}],"seen":{"c":1,"o":1,"d":1,"e":0},"seenHighlight":"e","labels":{"seen":"counts"}}}]},"options":[{"id":"return-false-now","label":"Return false right away","rationale":"e is at 0, so the magazine has no e left to give, and a note with a letter missing cannot be written.","isCorrect":true},{"id":"go-negative","label":"Use it anyway and let e drop to -1","rationale":"-1 would mean the magazine handed over a letter it never printed.","isCorrect":false},{"id":"skip-and-continue","label":"Skip this e and go on to the final d","rationale":"The answer has to cover every letter of the note. One refusal already makes it false.","isCorrect":false}]},{"id":"never-lowered-probe","kind":"case-probe","title":"Same input, but this version checks each count and never lowers it. What does it return?","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"second-e-never-lowered","state":{"arrays":[{"name":"ransomNote","values":["d","e","e","d"],"current":2},{"name":"magazine","values":["c","o","d","e","d"]}],"seen":{"c":1,"o":1,"d":2,"e":1},"seenHighlight":"e","labels":{"seen":"counts"}}}]},"options":[{"id":"true-and-wrong","label":"true, which is wrong: the single e passes the check twice","rationale":"e still reads 1 when the second e arrives, so one printed letter gets used for both. Without the lowering, the check only tests whether each letter appears in the magazine.","isCorrect":true},{"id":"false-check-catches","label":"false: the check still catches the second e","rationale":"The check reads the count for e, and nothing has changed that number since the magazine was counted.","isCorrect":false},{"id":"false-d-runs-out","label":"false: d runs out first","rationale":"d reads 2 for both of the note's d's, so it is never refused either.","isCorrect":false}]},{"id":"missing-letter-probe","kind":"case-probe","title":"In the second input, the words have swapped places, and the note starts with c. What should the function do?","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"c-has-no-entry","state":{"arrays":[{"name":"ransomNote","values":["c","e","d","e","d"],"current":0},{"name":"magazine","values":["d","e","e","d"]}],"seen":{"d":2,"e":2},"seenHighlight":"c","labels":{"seen":"counts"}}}]},"options":[{"id":"return-false-no-entry","label":"Return false: no entry means the magazine has 0 c's","rationale":"A letter that was never counted has nothing to give, the same as a letter whose count has reached 0.","isCorrect":true},{"id":"skip-unknown-letter","label":"Skip c, since only letters in the counts can be checked","rationale":"Skipping it would approve a note that needs a letter the magazine never printed.","isCorrect":false},{"id":"add-the-letter","label":"Add c to the counts at 1 and carry on","rationale":"The counts record what the magazine printed. Adding to them while reading the note invents a letter.","isCorrect":false}]},{"id":"ready-to-code","kind":"info","title":"Those were the last inputs to check, so the function comes next.","ctaLabel":"Start coding","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"before-counting","state":{"arrays":[{"name":"ransomNote","values":["d","e","e","d"]},{"name":"magazine","values":["c","e","d","e","d"]}],"seen":{},"labels":{"seen":"counts"}}}]},"body":["The function first reads the magazine once and counts how many copies of each letter it has.","Then it reads the note, one letter at a time. If a letter's count is 0, or the letter has no count at all, the function returns false right away. Otherwise the letter takes one copy, and its count goes down by 1.","If every letter of the note gets a copy, the function returns true."]},{"id":"coding","kind":"coding","source":"coding-checkpoints","title":"Count the magazine, then write the note","workspacePanel":"code"},{"id":"complete","kind":"complete","title":"A count of what the magazine can still give","workspacePanel":"code","takeaways":[{"title":"Count the magazine once","body":"After one read of the magazine, a single lookup in the counts says whether a letter is still available, so no letter of the note has to search."},{"title":"Using a copy lowers its count","body":"Each letter of the note that gets a copy lowers that count by 1, so the single e in \"coded\" is gone by the time the note reaches its second e."},{"title":"No entry and 0 are the same refusal","body":"A letter the magazine never printed and a letter whose copies are used up fail the same test, and the function returns false at that letter."},{"title":"Each string is read at most once","body":"For a magazine of m letters and a note of n, the time is O(m + n): 2,000 reads on two strings of 1,000 e's, where the search needed 500,500. The space is O(1), because there are at most 26 counts, one per lowercase letter."}]}],"slidesOrigin":"authored"}}
