{"id":"excel-sheet-column-number","title":"Excel Sheet Column Number","difficulty":"easy","patterns":["prefix-sum"],"primaryConcept":"array-prefix-sum-basics","concepts":["array-prefix-sum-basics"],"problemForm":"intro","companies":["Microsoft","Amazon","Google"],"category":"arrays-strings","executionStyle":"solution-method","compareMode":"exact","description":"Given a string `columnTitle` that represents a spreadsheet column title, return its corresponding column number.\n\nFor example, `A` maps to 1, `Z` maps to 26, `AA` maps to 27, and `AB` maps to 28.","examples":[{"input":"columnTitle = \"A\"","output":"1","explanation":"A is the first column."},{"input":"columnTitle = \"AB\"","output":"28","explanation":"A contributes one group of 26, then B contributes 2."},{"input":"columnTitle = \"ZY\"","output":"701","explanation":"Z contributes 26 groups of 26, then Y contributes 25."}],"constraints":[{"id":"1-len-columntitle-7","text":"1 <= len(columnTitle) <= 7","check":{"kind":"sizeRange","target":"columnTitle","measure":"stringLength","min":1,"max":7}},{"id":"columntitle-consists-only-of-uppercase-english-letters","text":"columnTitle consists only of uppercase English letters.","check":{"kind":"domain","target":"columnTitle","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":"columntitle-is-in-the-range-a-fxshrxw","text":"columnTitle is in the range [\"A\", \"FXSHRXW\"]."}],"starterCode":"class Solution:\n    def titleToNumber(self, columnTitle: str) -> int:\n        # Your code here\n        pass","functionName":"titleToNumber","testCases":[{"id":"1","input":{"columnTitle":"A"},"expected":1,"description":"Single first letter."},{"id":"2","input":{"columnTitle":"AB"},"expected":28,"description":"Two-letter title."},{"id":"3","input":{"columnTitle":"ZY"},"expected":701,"description":"Prompt example with a high leading letter."},{"id":"4","input":{"columnTitle":"AAA"},"expected":703,"description":"Three repeated letters."},{"id":"5","input":{"columnTitle":"AZ"},"expected":52,"description":"Last letter contributes 26."},{"id":"6","input":{"columnTitle":"FXSHRXW"},"expected":2147483647,"description":"Maximum value from the constraints."},{"id":"h1","input":{"columnTitle":"Z"},"expected":26,"description":"Single last letter."},{"id":"h2","input":{"columnTitle":"BA"},"expected":53,"description":"Second block starts at BA."},{"id":"h3","input":{"columnTitle":"ZZ"},"expected":702,"description":"Two high letters."},{"id":"h4","input":{"columnTitle":"ZZZ"},"expected":18278,"description":"Three maximum letters exercise the positional base-26 accumulation."}],"hints":[{"level":1,"content":"This is like reading a base-26 number, except A starts at 1 instead of 0.","revealPattern":false},{"level":2,"content":"Scan left to right. Before adding the next letter value, multiply the accumulated result by 26.","revealPattern":true},{"level":3,"content":"The value of a character is ord(ch) - ord('A') + 1.","revealPattern":true}],"guidedFlow":{"approachSelection":{"contract":"approach-selection-v1","question":"Which scan carries the least state?","decisionPrompt":"Choose the scan whose running state matches the shift-then-add rule directly.","approaches":[{"id":"positional-scan","name":"Left-to-right running total","summary":"Read left to right, multiply the running total by 26, then add the letter value.","decisionReason":"Reading left to right, the shift-then-add rule is exactly one running number, so no place counter is needed.","complexity":{"time":"O(n)","space":"O(1)"},"tradeoffs":["One running total, updated once per letter."],"isOptimal":true},{"id":"reverse-powers","name":"Right-to-left with powers","summary":"Read right to left, multiply each letter by a power of 26, and sum.","decisionReason":"Right-to-left powers of 26 produce the same column number in O(n) time. This flow scans left to right so the running total needs no separate power variable.","complexity":{"time":"O(n)","space":"O(1)"},"tradeoffs":["Carries an extra running power of 26 the forward scan avoids."],"isViable":true,"isOptimal":false},{"id":"lookup-table","name":"Precompute every title","summary":"Generate all possible titles in order and look the input up.","decisionReason":"There are 26^n titles of length n, so building the table costs exponential time and space for a value one scan produces.","complexity":{"time":"O(26^n)","space":"O(26^n)"},"tradeoffs":["Builds an exponential table for a number a single pass returns."],"isOptimal":false}]},"codingCheckpoints":[{"id":"init-total","title":"Initialize running total","description":"Start the numeric result at zero before scanning characters.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"array-scan","fact":"initializes_zero_accumulator","scope":"init-total","readMode":"array-scan-value-query-v1"}]},"commonMistakes":["Starting the result at one","Using an array when only one number is needed"],"hint":"The running total starts at 0."},{"id":"scan-title","title":"Scan each title character","description":"Process every character of the title from left to right.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"array-scan","fact":"scans_input_sequence","scope":"loop-through-strings","readMode":"array-scan-value-query-v1"}]},"commonMistakes":["Skipping the first character","Returning after processing only one character"],"hint":"Use one loop over columnTitle."},{"id":"update-total","title":"Apply base-26 update","description":"Shift the current total by 26 and add the current letter's 1-based value.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"array-scan","fact":"additively_updates_accumulator","scope":"update-total","readMode":"array-scan-value-query-v1"}]},"commonMistakes":["Treating A as 0","Adding values without multiplying the previous result by 26"],"hint":"result = result * 26 + value."},{"id":"return-total","title":"Return the final total","description":"Return the accumulated number after the scan finishes.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"array-scan","fact":"returns_terminal_accumulator","scope":"return-result","readMode":"array-scan-value-query-v1"}]},"commonMistakes":["Returning the last character value only","Returning before every character has been processed"],"hint":"Return the running result after the loop."}],"slides":[{"id":"read-the-problem","kind":"info","chapter":"Understand","eyebrow":"Read the problem","title":"Letters that count like a number","workspacePanel":"problem","body":["Spreadsheet columns are labeled A, B, ... Z, then AA, AB, and so on. Each title stands for a position: A is 1, Z is 26, AA is 27, AB is 28.","Given a title like \"AB\", return that position. \"AB\" is column 28."]},{"id":"count-up-to-title","kind":"code-trace","chapter":"Understand","eyebrow":"The obvious way","title":"Count titles until you reach it","summary":"Generating titles in order until one matches gets the right answer, but it counts every title up to the input one at a time. Watch how far that count has to climb.","workspacePanel":"trace","codeTrace":{"contract":"code-trace-v1","languageLabel":"Python","editorLanguage":"python","code":"count = 0\ntitle = \"\"\nwhile title != columnTitle:\n    count = count + 1\n    title = next_title(title)\nreturn count","typeContext":{"externalFunctions":{"next_title":{"params":["string"],"returns":"string"}}},"steps":[{"id":"first-title","title":"Count A as 1","codeLine":4,"summary":"columnTitle = \"AB\". The first title generated is \"A\", so count climbs to 1.","state":{"s":"A","count":1,"target":"AB","labels":{"s":"title","target":"columnTitle"}}},{"id":"walk-singles","title":"Walk through every single letter","codeLine":5,"summary":"B, C, on through Z: 26 titles generated and compared, none equal to \"AB\" yet.","state":{"s":"Z","count":26,"target":"AB","labels":{"s":"title","target":"columnTitle"}}},{"id":"reach-target","title":"\"AA\" then \"AB\" finally match","codeLine":3,"summary":"After Z comes AA (27), then AB (28). 28 titles were generated to reach a column 28 places in.","state":{"s":"AB","count":28,"target":"AB","labels":{"s":"title","target":"columnTitle"}}},{"id":"return-count","title":"Return the count, 28","codeLine":6,"summary":"title now equals \"AB\", so the loop stops and returns 28, the number of titles walked.","state":{"s":"AB","count":28,"target":"AB","labels":{"s":"title","target":"columnTitle"}}}],"localizations":{"javascript":{"languageLabel":"JavaScript","editorLanguage":"javascript","codeStyle":"natural","code":"let count = 0;\nlet title = \"\";\nwhile (title !== columnTitle) {\n    count = count + 1;\n    title = nextTitle(title);\n}\nreturn count;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":7},"statementBundles":{"5":{"startLine":5,"endLine":6}}},"typescript":{"languageLabel":"TypeScript","editorLanguage":"typescript","codeStyle":"natural","code":"let count = 0;\nlet title = \"\";\nwhile (title !== columnTitle) {\n    count = count + 1;\n    title = nextTitle(title);\n}\nreturn count;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":7},"statementBundles":{"5":{"startLine":5,"endLine":6}}},"java":{"languageLabel":"Java","editorLanguage":"java","codeStyle":"natural","code":"int count = 0;\nString title = \"\";\nwhile (!Objects.equals(title, columnTitle)) {\n    count = count + 1;\n    title = nextTitle(title);\n}\nreturn count;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":7},"statementBundles":{"5":{"startLine":5,"endLine":6}}},"csharp":{"languageLabel":"C#","editorLanguage":"csharp","codeStyle":"natural","code":"int count = 0;\nstring title = \"\";\nwhile (title != columnTitle) {\n    count = count + 1;\n    title = NextTitle(title);\n}\nreturn count;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":7},"statementBundles":{"5":{"startLine":5,"endLine":6}}},"cpp":{"languageLabel":"C++","editorLanguage":"cpp","codeStyle":"natural","code":"int count = 0;\nstring title = \"\";\nwhile (title != column_title) {\n    count = count + 1;\n    title = next_title(title);\n}\nreturn count;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":5,"6":7},"statementBundles":{"5":{"startLine":5,"endLine":6}}}}},"body":["Generate column titles in order, A, B, on up, counting each one until the generated title matches the input. The count when they match is the answer."]},{"id":"counting-cost","kind":"case-probe","chapter":"Understand","eyebrow":"Name the cost","title":"How many titles does this generate for \"FXSHRXW\"?","workspacePanel":"visualizer","summary":"\"FXSHRXW\" is the largest allowed title.","options":[{"id":"all-the-way","label":"Every title from A through it, over two billion of them","rationale":"The loop counts one title per step until the generated title matches, so it walks all 2,147,483,647 titles from A up to \"FXSHRXW\", one at a time.","isCorrect":true},{"id":"seven","label":"Seven, one per letter","rationale":"Seven is only the input length. The generator still has to enumerate the target's column number, which is over two billion."},{"id":"twenty-six","label":"Twenty-six, one full block","rationale":"A single block of 26 covers only the one-letter titles. \"FXSHRXW\" sits billions of titles past Z, and each is generated and compared."}],"answer":"The brute-force generator would produce every title from A through FXSHRXW. That is more than two billion generated titles for a seven-letter input.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"probe","state":{"s":"FXSHRXW","target":"FXSHRXW","count":"?","labels":{"s":"title","target":"columnTitle"}}}]}},{"id":"leading-letter-weight","kind":"case-probe","chapter":"Understand","eyebrow":"Find the rule","title":"In \"AB\", how much does the leading A stand for on its own?","workspacePanel":"visualizer","summary":"A alone is column 1, but \"AB\" is column 28.","options":[{"id":"twenty-six","label":"26, a full block of letters","rationale":"A in the second-from-right spot leads a whole run of 26 single-letter columns (A through Z) before AA begins, so it carries 26, and 26 + 2 gives 28.","isCorrect":true},{"id":"one","label":"1, the same as the column A","rationale":"A by itself is 1, but inside \"AB\" the A sits one place to the left, so it represents one full block of 26 columns."},{"id":"two","label":"2, one less than B","rationale":"The leading letter's worth comes from its place: the run of 26 columns it leads."}],"answer":"The A leads a block of 26 columns, so it stands for 26; add B for 2 and the title is 28.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"state","state":{"s":"AB","i":0,"labels":{"s":"columnTitle","i":"ch"},"result":"?"}}]}},{"id":"shift-then-add","kind":"case-probe","chapter":"Understand","eyebrow":"Find the rule","title":"Reading A gives a running total of 1. What happens to that 1 when B arrives?","workspacePanel":"visualizer","summary":"The running total is 1 after A; \"AB\" must reach 28.","options":[{"id":"times-26","label":"It is multiplied by 26, then B is added","rationale":"Moving one letter right pushes everything read so far up by a full block of 26, so 1 becomes 26, then adding B (2) lands on 28.","isCorrect":true},{"id":"plus-only","label":"B is just added to it, giving 3","rationale":"1 + 2 is 3, but that treats both letters as the same place. The earlier total has to shift up by 26 first, so the real total is 28."},{"id":"times-2","label":"It is doubled, then B is added","rationale":"Doubling has no basis here. Each new letter scales the previous total by the alphabet size, 26."}],"answer":"Multiply the running 1 by 26 to get 26, then add B for 2: the total becomes 28. Every new letter shifts the old total up by one base-26 place.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"state","state":{"s":"AB","i":1,"used":[0],"labels":{"s":"columnTitle","i":"ch"},"result":1}}]}},{"id":"approach-selection","kind":"choice","source":"approach-selection","chapter":"Understand","eyebrow":"Pick the route","title":"Which scan carries the least state?","workspacePanel":"visualizer","selection":{"contract":"approach-selection-v1","question":"Which scan carries the least state?","decisionPrompt":"Choose the scan whose running state matches the shift-then-add rule directly.","approaches":[{"id":"positional-scan","name":"Left-to-right running total","summary":"Read left to right, multiply the running total by 26, then add the letter value.","decisionReason":"Reading left to right, the shift-then-add rule is exactly one running number, so no place counter is needed.","complexity":{"time":"O(n)","space":"O(1)"},"tradeoffs":["One running total, updated once per letter."],"isOptimal":true},{"id":"reverse-powers","name":"Right-to-left with powers","summary":"Read right to left, multiply each letter by a power of 26, and sum.","decisionReason":"Right-to-left powers of 26 produce the same column number in O(n) time. This flow scans left to right so the running total needs no separate power variable.","complexity":{"time":"O(n)","space":"O(1)"},"tradeoffs":["Carries an extra running power of 26 the forward scan avoids."],"isViable":true,"isOptimal":false},{"id":"lookup-table","name":"Precompute every title","summary":"Generate all possible titles in order and look the input up.","decisionReason":"There are 26^n titles of length n, so building the table costs exponential time and space for a value one scan produces.","complexity":{"time":"O(26^n)","space":"O(26^n)"},"tradeoffs":["Builds an exponential table for a number a single pass returns."],"isOptimal":false}]},"visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"state","state":{"s":"AB","i":0,"labels":{"s":"columnTitle","i":"ch"},"result":0}}]}},{"id":"dry-run-setup","kind":"info","chapter":"Trace","eyebrow":"Watch it build","title":"One running total, one letter at a time","workspacePanel":"visualizer","body":["The earned rule is one line: carry a single running total, and for each letter multiply that total by 26 before adding the letter value (A is 1, B is 2, up to Z is 26).","Walk \"AB\" one letter at a time and predict the running total at each stop, watching the shift-then-add do all the work."],"visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"state","state":{"s":"AB","labels":{"s":"columnTitle"}}}]}},{"id":"trace","kind":"dry-run","source":"trace-example","chapter":"Trace","eyebrow":"Build the total","title":"Convert \"AB\"","workspacePanel":"visualizer","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"setup","state":{"s":"AB","labels":{"s":"columnTitle"}}},{"id":"read-a","state":{"s":"AB","i":0,"result":0,"labels":{"s":"columnTitle","i":"ch"}}},{"id":"read-b","state":{"s":"AB","i":1,"result":1,"labels":{"s":"columnTitle","i":"ch"}}},{"id":"finish","state":{"s":"AB","i":1,"result":28,"labels":{"s":"columnTitle","i":"ch"}}}]},"beats":[{"id":"setup","title":"Before any letter is read, what is the running total?","summary":"The scan is about to begin on \"AB\".","prompt":"Nothing has been read yet.","options":[{"id":"zero","label":"0","rationale":"An empty total has read nothing, so it starts at 0. The first shift-and-add then builds on a clean slate.","isCorrect":true},{"id":"one","label":"1","rationale":"Starting at 1 would add a phantom column. The total holds the value of the letters read, and none have been read yet.","isCorrect":false}],"answer":"0. The running total starts empty and the first letter fills it."},{"id":"read-a","title":"A is read with the total at 0. What is the total now?","summary":"A arrives first, with nothing to its left.","prompt":"Multiply the total by 26, then add A's value.","options":[{"id":"one","label":"1","rationale":"0 times 26 is 0, plus A (1) is 1. With nothing before it, the shift does nothing and A simply lands as 1.","isCorrect":true},{"id":"twenty-six","label":"26","rationale":"A is worth 1 at this point. The 26 weight appears only once a later letter shifts this total to its left.","isCorrect":false},{"id":"twenty-seven","label":"27","rationale":"27 is AA, two letters. After only A the total is 1.","isCorrect":false}],"answer":"1. 0 times 26 is 0, plus 1 for A, so the total is 1."},{"id":"read-b","title":"B is read with the total at 1. What is the total now?","summary":"B arrives after A, one place to the right.","prompt":"Apply the same shift-then-add to the total of 1.","options":[{"id":"twenty-eight","label":"28","rationale":"The running 1 first scales up a full block, 1 times 26 is 26, then B (2) is added for 28.","isCorrect":true},{"id":"three","label":"3","rationale":"1 + 2 is 3, but that leaves the A in B's place. The earlier total has to shift up by 26 first, so the real total is 28.","isCorrect":false},{"id":"two","label":"2","rationale":"That drops the A entirely. The running total carries A forward, scaled by 26, before B is added.","isCorrect":false}],"answer":"28. The running 1 scales to 26, then 2 for B is added. The shift is what separates it from a plain 3."},{"id":"finish","title":"The scan is done. Which stored value should the function return?","summary":"The scan has reached the end of \"AB\".","prompt":"The loop has finished its last letter.","options":[{"id":"total","label":"The running total","rationale":"Every letter has already been shifted and added into the running total, so it now holds the finished column number.","isCorrect":true},{"id":"last-value","label":"The last letter value","rationale":"The last letter value is only B on its own. The accumulated total is the answer the whole scan was building.","isCorrect":false}],"answer":"Return the running total, which now holds the finished column number after the last letter."}]},{"id":"a-as-zero-pitfall","kind":"pitfall","chapter":"Trace","eyebrow":"Spot the trap","title":"Can the letters be numbered from 0 like array indices?","workspacePanel":"trace","summary":"Subtracting the position of A in the alphabet gives A is 0, B is 1, the clean zero-based numbering arrays use. The shift-and-add loop still runs and still returns a number. Watch it convert \"AB\".","pitfall":"The code below values A as 0 with ord(ch) - ord(\"A\").","whyItMatters":"With A worth 0, the leading A contributes 0 times 26, so the whole leading block vanishes and \"AB\" returns 1 instead of 28. Excel columns are 1-based: A must be 1, so the value is ord(ch) - ord(\"A\") + 1.","negativeTrace":{"contract":"negative-trace-v1","languageLabel":"Python","editorLanguage":"python","code":"result = 0\nfor ch in columnTitle:\n    value = ord(ch) - ord(\"A\")\n    result = result * 26 + value\nreturn result","steps":[{"id":"read-a-zero","title":"A is valued as 0","codeLine":3,"summary":"\"AB\", reading A. ord(\"A\") - ord(\"A\") is 0, so A contributes nothing.","state":{"s":"AB","i":0,"value":0,"result":0,"labels":{"s":"columnTitle","i":"ch"}}},{"id":"update-a","title":"The leading A leaves the total at 0","codeLine":4,"summary":"0 times 26 plus 0 is still 0. The leading block is gone before B is even read.","state":{"s":"AB","i":0,"value":0,"result":0,"labels":{"s":"columnTitle","i":"ch"}}},{"id":"read-b","title":"B is valued as 1","codeLine":3,"summary":"Reading B. ord(\"B\") - ord(\"A\") is 1, one short of its true value of 2.","state":{"s":"AB","i":1,"value":1,"result":0,"labels":{"s":"columnTitle","i":"ch"}}},{"id":"wrong-result","title":"\"AB\" returns 1, not 28","codeLine":5,"summary":"0 times 26 plus 1 is 1. The A was erased and B was undercounted.","state":{"s":"AB","i":1,"value":1,"result":1,"labels":{"s":"columnTitle","i":"ch"}}}],"localizations":{"javascript":{"languageLabel":"JavaScript","editorLanguage":"javascript","codeStyle":"natural","code":"let result = 0;\nfor (const ch of columnTitle) {\n    const value = ch.charCodeAt(0) - 65;\n    result = (result * 26) + value;\n}\nreturn result;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":6},"statementBundles":{"4":{"startLine":4,"endLine":5}}},"typescript":{"languageLabel":"TypeScript","editorLanguage":"typescript","codeStyle":"natural","code":"let result = 0;\nfor (const ch of columnTitle) {\n    const value = ch.charCodeAt(0) - 65;\n    result = (result * 26) + value;\n}\nreturn result;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":6},"statementBundles":{"4":{"startLine":4,"endLine":5}}},"java":{"languageLabel":"Java","editorLanguage":"java","codeStyle":"natural","code":"int result = 0;\nfor (char ch : columnTitle.toCharArray()) {\n    int value = ((int)String.valueOf(ch).charAt(0)) - 65;\n    result = (result * 26) + value;\n}\nreturn result;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":6},"statementBundles":{"4":{"startLine":4,"endLine":5}}},"csharp":{"languageLabel":"C#","editorLanguage":"csharp","codeStyle":"natural","code":"int result = 0;\nforeach (char ch in columnTitle) {\n    int value = Convert.ToInt32(ch) - 65;\n    result = result * 26 + value;\n}\nreturn result;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":6},"statementBundles":{"4":{"startLine":4,"endLine":5}}},"cpp":{"languageLabel":"C++","editorLanguage":"cpp","codeStyle":"natural","code":"int result = 0;\nfor (char ch : column_title) {\n    int value = static_cast<int>(ch) - 65;\n    result = (result * 26) + value;\n}\nreturn result;","lineMap":{"1":1,"2":2,"3":3,"4":4,"5":6},"statementBundles":{"4":{"startLine":4,"endLine":5}}}}}},{"id":"edge-intro","kind":"info","chapter":"Edge cases","eyebrow":"Stress the rule","title":"Which titles could break the running total?","workspacePanel":"visualizer","body":["The rule is one line: shift the total by 26, add the letter value, repeat. The next two titles probe whether that single line needs any special branches.","Try a title with just one letter, then one near the longest the constraints allow."],"visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"edge-start","state":{"s":"Z","i":0,"result":0,"labels":{"s":"columnTitle","i":"ch"}}}]}},{"id":"edge-single-letter","kind":"case-probe","chapter":"Edge cases","eyebrow":"Stress the rule","title":"Does a one-letter title like \"Z\" need its own branch?","workspacePanel":"visualizer","summary":"columnTitle = \"Z\". There is no earlier letter to shift.","options":[{"id":"no-branch","label":"No, the same shift-and-add handles it","rationale":"0 times 26 plus 26 is 26. With nothing to its left, the multiply hits a total of 0 and does nothing, so the single letter just lands as its own value.","isCorrect":true},{"id":"special","label":"Yes, skip the multiply when there is one letter","rationale":"No skip is needed: the multiply by 26 hits a total of 0 and does nothing on its own, so the one-letter case falls out of the same line."},{"id":"wrong-value","label":"No, but the lone letter forces a return of 1","rationale":"Running the loop once does not pin the answer at 1. Z is worth 26, and 0 times 26 plus 26 lands on 26."}],"answer":"No branch. 0 times 26 plus 26 is 26, so a single letter is handled by the same line as any longer title.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"single","state":{"s":"Z","i":0,"result":0,"value":26,"labels":{"s":"columnTitle","i":"ch"}}}]}},{"id":"edge-long-title","kind":"case-probe","chapter":"Edge cases","eyebrow":"Stress the rule","title":"For a 7-letter title like \"FXSHRXW\", does the rule change?","workspacePanel":"visualizer","summary":"\"FXSHRXW\" is the longest title the constraints allow.","options":[{"id":"same-rule","label":"No, seven repeats of shift-and-add settle it","rationale":"Length only means more iterations of the same line; each letter shifts the total by 26 and adds, exactly as on two letters.","isCorrect":true},{"id":"precompute","label":"Yes, precompute a fixed-width formula for long titles","rationale":"A fixed-width shortcut adds a special case for no reason. The loop already scales each place correctly by repeating the multiply."},{"id":"chunk","label":"Yes, split it into shorter pieces first","rationale":"Splitting breaks the place values between pieces. The single running total already carries every place across the whole title."}],"answer":"No change. A longer title is just more turns of the same shift-and-add; the running total scales each place on its own.","visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"long","state":{"s":"FXSHRXW","i":6,"used":[0,1,2,3,4,5],"labels":{"s":"columnTitle","i":"ch"}}}]}},{"id":"ready-to-code","kind":"info","chapter":"Code","eyebrow":"Get ready to code","title":"Turn the rule into code","workspacePanel":"visualizer","summary":"The shift by 26 is the move; the +1 on each letter is the detail to get right.","ctaLabel":"Start coding","body":["Start a running total at 0 and walk the title left to right. For each letter, shift the total up by a block of 26 and add the letter value, then return the total once the scan ends.","The letter value is 1-based: A is 1, so the leading position is never erased."],"visualTrace":{"contract":"guided-visual-trace-v1","steps":[{"id":"state","state":{"s":"AB","i":0,"labels":{"s":"columnTitle","i":"ch"},"result":0}}]}},{"id":"coding","kind":"coding","source":"coding-checkpoints","chapter":"Code","eyebrow":"Code the rule","title":"Build the running total","summary":"Now turn the shift-then-add into code: start the total at 0, scan the title left to right, and for each letter multiply the total by 26 and add the letter value, then return the total once the scan ends.","workspacePanel":"code","checkpoints":[{"id":"init-total","title":"Initialize running total","description":"Start the numeric result at zero before scanning characters.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"array-scan","fact":"initializes_zero_accumulator","scope":"init-total","readMode":"array-scan-value-query-v1"}]},"commonMistakes":["Starting the result at one","Using an array when only one number is needed"],"hint":"The running total starts at 0."},{"id":"scan-title","title":"Scan each title character","description":"Process every character of the title from left to right.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"array-scan","fact":"scans_input_sequence","scope":"loop-through-strings","readMode":"array-scan-value-query-v1"}]},"commonMistakes":["Skipping the first character","Returning after processing only one character"],"hint":"Use one loop over columnTitle."},{"id":"update-total","title":"Apply the shift-and-add","description":"Multiply the current total by 26 and add the current letter value (A is 1).","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"array-scan","fact":"additively_updates_accumulator","scope":"update-total","readMode":"array-scan-value-query-v1"}]},"commonMistakes":["Valuing A as 0","Adding the letter value without first multiplying the total by 26"],"hint":"result = result * 26 + value, where value runs from 1 for A to 26 for Z."},{"id":"return-total","title":"Return the final total","description":"Return the accumulated number after the scan finishes.","validation":{"type":"semantic","validator":"familyFact","params":[{"family":"array-scan","fact":"returns_terminal_accumulator","scope":"return-result","readMode":"array-scan-value-query-v1"}]},"commonMistakes":["Returning the last letter value only","Returning before every character has been processed"],"hint":"Return the running result after the loop."}]},{"id":"complete","kind":"complete","chapter":"Code","eyebrow":"Wrap up","title":"What to carry forward","workspacePanel":"code","summary":"One running total, shifted by 26 and topped up by each letter, is the whole solution.","body":["A column title is a base-26 number with letters for digits, and A starts at 1.","Carry one running total: each letter multiplies it by 26 and adds the letter value, so a single left-to-right pass returns the column number."],"takeaways":[{"title":"Each letter shifts the total up a block of 26","body":"Moving one place left multiplies the running total by 26, which is why \"AB\" is 28."},{"title":"A starts at 1","body":"The letter value is ord(ch) - ord(\"A\") + 1, so the leading position is never erased the way zero-based numbering would erase it."},{"title":"One number, one pass, O(n) time and O(1) space","body":"The total is the only state, updated once per letter, so the scan is linear in the title length with constant extra space."}],"nextAction":"Submit when you are ready. TraceCode will run the final checks against the code you wrote."}],"slidesOrigin":"authored","version":2}}
