Microsoft Coding Interview Question! | Excel Sheet Column Number - Leetcode 171

preview_player
Показать описание
FAANG Coding Interviews / Data Structures and Algorithms / Leetcode
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms For FREE at AlgoMap.io!

GregHogg
Автор

A different approach to this is starting from the start of the string and using the ascii conversion, char - 64, that A would be equal to 1, B to 2 and so on. We would enter with a sum = 0 and every loop we multiply sum by 26 and add the new letters value, similar to a conversion from a base 26 number system to decimal.

georgetolis
Автор

If we assume it is a 26 base number and instead of using a hash map, we can just use the (ASCII - 65) to get the value of that position, it's just the same as binary to decimal conversion and add 1 at end

raghavwastaken
Автор

res = 0
for d in columnTitle:
res = 26 * res + ord(d) - ord('A') + 1
return res

davidespinosa
Автор

For x in range(1, 1001):
Print (' thank you for the content ')

Spartan-gow
Автор

why a hashmap? you can literally use the ascii value to calculate it. Example in python:
def to_number(code: str) -> int:
total = 0
length = len(code)
A = ord('A')
Z = ord('Z')
for idx, chr in enumerate(code):
c = ord(chr.upper())
if c < A or c > Z: return -1 # Invalid input
num = c - A + 1
total += 26 ** (length - (idx + 1)) * num
return total

you can also iterate through it backwards if you don't want to do (length - (idx + 1)) and instead want to do (idx)

mrt_
Автор

I would use a spightly different approach, skipping the hashmap step

If you convert each character to their ascii representative, subtract 65 (ascii for capital A), then handle it as a number in base 26 you need to convert to base 10

beetaylor
Автор

On a side note: If it is really referring to Excel columnames, then there is a maxvalue of "XFD" (= 2 ^ 14). But the basic principle could be extended.

Arrow
Автор

Instead of going backwards you could also apply horners method

niklashaas
Автор

This is the same logic when encoding a shuffle of deck of cards into a number. So you can have shuffle 1, 2, 3, ... 52! and each number corresponds to a specific configuration of a deck.

senior
Автор

A lot of people have already pointed out the ASCII conversion, but here's the code if anyone was wondering. Sorry it's in Java. I'm more accustomed to it. :P
I'm also aware of the limitations due to short, but if you have more than 32, 767 columns, what are you doing?

String columnTitle = "ZY";
short num = 0, m = 1;
int i = columnTitle.length();
while (i > 0) {
num += (columnTitle.charAt(--i) - 64) * m;
m *= 26;
}
System.out.println(num); //Outputs 701

dial-upking
Автор

Instead of going backwards you can multiply m by (length - pos)

robertsandiford
Автор

Wouldnt using a simple 29 element array would be better, or just using ascii convertions?

GM-reaper
Автор

Wouldn't it be easier to treat the string as a base 26 number system? I mean I get that's what the answer is gonna get ya no matter what but I always do my different base number systems from the highest place value

pierceclemmons
Автор

For mentioning hash map you will get huge minus on coding interview. Iterating backwards is also will likely give you a minus

Sprezzatura
Автор

Not surprised to hear it’s easy to get into Microsoft. 😂😂😂😂

Darkpill-
Автор

Why are you starting from the back, is this a question with: * use a hashmap? or similar... maybe is just me but i cannot see why would you start from the back.

IsaacAlcocer
Автор

You forgot to mention it's just 26 conversation

amirdashti
Автор

Interviewer: Let's say you have a problem with your manager. How are you...
Me: I'll use a hash map!

ichiroutakashima
Автор

Unintutive trick? Maybe if you never went to a maths class

phill