Excel Sheet Column Problem

Introduction

In this algorithm challenge on LeetCode, I decided to tackle the Excell Column Sheet Problem. This problem required me to take an alphabet string that represented integer values, for example, the letter “A” would represent 1, “B” would represent 2, and so forth. However, there is a particular trick to this problem. If the string happened to be “AA” it would result in 27. The base number, which is 26 is added to the string value. For an understanding, let’s break it down below.

Input: columnTitle = "A"
Output: 1
Input: columnTitle = "AB"
Output: 28
because 1 * 26 + 2 = 28. Remember 26 is our base number when having more than one characters.

The Breakdown

My initial thought process was to store the letters into objects with key-value pairs. This would allow me to connect the letter and number to the string in my arguments. Here is a better look below.

function titleToNumber(s){let letters = {
A: 1, B: 2, C: 3, D: 4, E: 5, F: 6, G: 7, H: 8,
I: 9, J: 10, K: 11, L: 12, M: 13, N: 14, O: 15,
P: 16, Q: 17, R: 18, S: 19, T: 20, U: 21, V: 22,
W: 23, X: 24, Y: 25, Z: 26
}
}

I created a variable called num and set it to zero. This would later be used to increment the number and return it.

function titleToNumber(s){let letters = {
A: 1, B: 2, C: 3, D: 4, E: 5, F: 6, G: 7, H: 8,
I: 9, J: 10, K: 11, L: 12, M: 13, N: 14, O: 15,
P: 16, Q: 17, R: 18, S: 19, T: 20, U: 21, V: 22,
W: 23, X: 24, Y: 25, Z: 26
}
let num = 0}

Furthermore, I looped through the entire string. Within the loop, I decided to multiply the num to the base and add it to the object of the current index. Here is a better look below.

function titleToNumber(s){let letters = {
A: 1, B: 2, C: 3, D: 4, E: 5, F: 6, G: 7, H: 8,
I: 9, J: 10, K: 11, L: 12, M: 13, N: 14, O: 15,
P: 16, Q: 17, R: 18, S: 19, T: 20, U: 21, V: 22,
W: 23, X: 24, Y: 25, Z: 26
}
let num = 0for(let i = 0; i < s.length; i++){
num = num * 26 + letters[s[i]]
}
}

Once those steps were completed I simply return the num. Here is a full look below.

function titleToNumber(s){let letters = {
A: 1, B: 2, C: 3, D: 4, E: 5, F: 6, G: 7, H: 8,
I: 9, J: 10, K: 11, L: 12, M: 13, N: 14, O: 15,
P: 16, Q: 17, R: 18, S: 19, T: 20, U: 21, V: 22,
W: 23, X: 24, Y: 25, Z: 26
}
let num = 0for(let i = 0; i < s.length; i++){
num = num * 26 + letters[s[i]]
}
return num}console.log(titleToNumber("A"))

Thanks for reading. if there are any questions just post in the comment?

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Build a hackernews clone with authentication using React, GraphQL and 8base

缉魂~(𝟐𝟎𝟐𝟎-𝐙𝐇)~新的完整版本~【𝐓𝐡𝐞 𝐒𝐨𝐮𝐥】𝐇𝐃

day 4 of my #1000daysofcoding

Debugging polyglot Node.js, Ruby, R apps with GraalVM

Basics of Javascript · String · toUpperCase() (method)

Why adding Key is important in ReactJS? Let’s Find Out !

React Native ERROR: Failed to build gem native extension.

React Native App Development: Key Factors that Minimize the Development Costs!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Woodelin florveus

Woodelin florveus

More from Medium

A dread of working after vacation

Need of different Sorting Techniques

How to Hire Full Stack Developers: What You Need to Know

Basic OOP for shavetail Louies — PART II [Abstraction]