Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
Given a string columnTitle that represents the column title as appears in an Excel sheet, return its corresponding column number.
For example:
A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...
Example 1:
Input: columnTitle = "A" Output: 1
Example 2:
Input: columnTitle = "AB" Output: 28
Example 3:
Input: columnTitle = "ZY" Output: 701
Constraints:
1 <= columnTitle.length <= 7columnTitle consists only of uppercase English letters.columnTitle is in the range ["A", "FXSHRXW"].Problem summary: Given a string columnTitle that represents the column title as appears in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
"A"
"AB"
"ZY"
excel-sheet-column-title)cells-in-a-range-on-an-excel-sheet)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #171: Excel Sheet Column Number
class Solution {
public int titleToNumber(String columnTitle) {
int ans = 0;
for (int i = 0; i < columnTitle.length(); ++i) {
ans = ans * 26 + (columnTitle.charAt(i) - 'A' + 1);
}
return ans;
}
}
// Accepted solution for LeetCode #171: Excel Sheet Column Number
func titleToNumber(columnTitle string) (ans int) {
for _, c := range columnTitle {
ans = ans*26 + int(c-'A'+1)
}
return
}
# Accepted solution for LeetCode #171: Excel Sheet Column Number
class Solution:
def titleToNumber(self, columnTitle: str) -> int:
ans = 0
for c in map(ord, columnTitle):
ans = ans * 26 + c - ord("A") + 1
return ans
// Accepted solution for LeetCode #171: Excel Sheet Column Number
struct Solution;
impl Solution {
fn title_to_number(s: String) -> i32 {
s.bytes()
.fold(0, |sum, c| sum * 26 + i32::from(c) - 'A' as i32 + 1)
}
}
#[test]
fn test() {
assert_eq!(Solution::title_to_number("A".to_string()), 1);
assert_eq!(Solution::title_to_number("AB".to_string()), 28);
assert_eq!(Solution::title_to_number("ZY".to_string()), 701);
}
// Accepted solution for LeetCode #171: Excel Sheet Column Number
function titleToNumber(columnTitle: string): number {
let ans: number = 0;
for (const c of columnTitle) {
ans = ans * 26 + (c.charCodeAt(0) - 'A'.charCodeAt(0) + 1);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.