Convert Excel Column Title to Number

Duration: 7 min

This video lesson is available to enrolled students.

Enroll to watch — ACCENTURE Superset

AI summary & chapters

AI Summary

An AI-generated summary of this video lecture.

This educational video from Knowledge Gate provides a comprehensive tutorial on solving a programming problem related to converting an Excel column title to its corresponding column number. The lecture begins by defining the problem: given a string representing an Excel column title (e.g., 'A', 'AA', 'AAA'), find the integer that represents that column. The core of the video is a step-by-step derivation of the algorithm, which is based on a base-26 number system where 'A' represents 1, 'B' represents 2, and so on, up to 'Z' representing 26. The instructor uses a chalkboard to demonstrate the mathematical logic, showing that for a string like 'AA', the calculation is (1 * 26) + 1 = 27. This is generalized into a formula: result = 26 * result + (s[i] - 'A' + 1). The video then transitions to a code implementation in C, showing a function `titleToNumber` that iterates through the string, applying this formula to build the final column number. The lesson is reinforced with multiple worked examples, including the conversion of 'A' to 1, 'AA' to 27, and 'AAC' to 705, which are shown on the board and in the code.

Chapters

  1. 0:00 2:00 00:00-02:00

    The video opens with the Knowledge Gate logo, followed by a slide that introduces the problem. The problem statement is displayed on a whiteboard, which reads: 'Given a string S that represents column title of an Excel sheet, find the number that represents that column. In excel A column is number 1, AA is 27 and so on.' The constraints are listed as 1 ≤ |S| ≤ 7. The input is a single line containing string S, and the output is the column number. An example is provided: Input 'A' outputs 1, and Input 'AA' outputs 27. The instructor, Yash Jain Sir, is visible in a small window, and the video is marked as a copyright of Knowledge Gate Eduventures.

  2. 2:00 5:00 02:00-05:00

    The instructor explains the pattern of Excel column titles, which is a base-26 system. He writes on the chalkboard that column 1 is 'A', column 2 is 'B', ..., column 26 is 'Z', column 27 is 'AA', and so on. He provides a table of examples, showing that 26 maps to 'Z', 51 to 'AY', 52 to 'AZ', 80 to 'CB', 676 to 'ZZ', and 705 to 'AAC'. He then begins to derive the formula for the conversion, writing 'Z -> 26' and 'AA -> 27'. He explains that the first 'A' in 'AA' is in the 26's place, so the calculation is (1 * 26) + 1 = 27. He continues to break down the logic for 'AAC', showing that it is (1 * 26^2) + (1 * 26^1) + (3 * 26^0) = 676 + 26 + 3 = 705. He emphasizes that the formula is 'result = 26 * result + (s[i] - 'A' + 1)'.

  3. 5:00 6:41 05:00-06:41

    The video transitions to a code implementation. A code editor window displays a C function `int titleToNumber(char s[])`. The function initializes an integer `result` to 0. A for loop iterates through each character of the string `s`. Inside the loop, the formula `result = result * 26 + s[i] - 'A' + 1;` is applied. The instructor explains that this formula is derived from the base-26 logic. He then shows the function returning the final `result`. The video concludes by revisiting the examples on the board, confirming that the code correctly handles inputs like 'A' (output 1) and 'AA' (output 27), reinforcing the connection between the mathematical concept and its programming implementation.

The video presents a clear, structured lesson on a classic programming problem. It effectively bridges the gap between a real-world application (Excel column naming) and its underlying mathematical algorithm. The teaching progression moves logically from problem definition to conceptual understanding (base-26 system) to algorithmic derivation (the formula) and finally to practical implementation (the C code). The use of a chalkboard for step-by-step calculations and a code editor for the final solution provides a comprehensive learning experience, making the abstract concept of number systems tangible for students.

Loading lesson…