Excel Column Title | InterviewBit | Solution Explained

Excel Column Title is a math problem on InterviewBit. I explain the solution using a simple approach. Problem Link.

This solution is the inverse of: Excel Column Number | InterviewBit | Solution Explained

0. Excel Column Title: Problem Discussion

0.0. IO Format

  • input:
    • an integer
  • output:
    • a string of characters, which is the equivalent of the input integer
  • constraints:
    • characters output capital English letters
    • solution must be better than O(N) time and space each

0.1. Examples

The problem statement is weird, so let’s take a look at some examples.

  • 1 -> A
  • 2 -> B
  • 26 -> Z
  • 27 -> AA
  • 52 -> AZ

And so on.

1. Excel Column Title: Observations & Logic

1.0. Intuitions

It is clear that we map integers from 1 to 26 to A to Z respectively and then from 27, we add one more A, at the string position 1 with A at position 0, to create 27. That is, 27 = 261 * val(A) + 260 * val(A).

1.1. Building Upon the Intuitions

We notice that to extract out the characters from the integer, we need to be able to break it into powers of 26. But that already sounds a bit cumbersome, yeah?

So let’s make life easy. First, instead of bothering with the appropriate power of 26, let’s extract the remainder of 26. Then, we will set the number to be number/26.

1.2. Example Explanation

Confused? Let’s take the case of 352569. First, we get the remainder with 26 = 9. This means that the letter is “I” (capital i). Then we divide 352569 by 26 = 13560.

Now, we do the same.

  • Extract the remainder: 13560%26 = 14
  • Map the remainder: N
  • Divide the number: 13560/26 = 521

And again –

  • Extract the remainder: 521 % 26 = 1
  • Map the remainder: A
  • Divide the number: 521/26 = 20

And again –

  • Extract the remainder: 20 % 26 = 20
  • Map the remainder: T
  • Divide the number: 20/26 = 0

Since the number is now 0, we are done!

2. Excel Column Title: Implementation

2.0. Code

class Solution:
    def convertToTitle(self, A):
        ans = ""
        while A:
            A -= 1
            ans += chr(ord('A') + A % 26)
            A //= 26
        return ans[::-1]

2.1. Complexity Analysis

  • Time: O(N), to iterate over all the elements in the string, where N is the length of the input string.
  • Space: O(N), since reversing the string in python creates a new copy of it, and thus takes O(N) space.
Avatar for Tanishq Chaudhary

Producing high-quality intuitive explanations of interview problems. Currently covering LeetCode and InterviewBit.

    Comments

    1. Please write the proof.

    2. This solution need an amendment for 2 digit number.

    3. Please write a Proof for this

    4. Matt Michael D’Agati is the founder of Renewables Worldwide, an Solar Firm in MA.

      A few age ago, taking a leap of faith, Matt D’Agati delved into the realm of solar, and/or in a short occasion commenced effectively marketing significant amounts of power, primarily around the business industry, collaborating with solar farm developers and local businesses in the “design” of their unique plans.

      Ongoing marketing within the sector, offered Matthew to participate a regional startup two years in the, and in a short time, he assumed the role of their Chief Strategy Officer, in charge of all operation and endeavor improvement, in addition to being marketed social group property.

      To planned unions and shear get the job done mentality, Matthew D’Agati brought that firm from an initial initial-year revenue to in excess of a 2 hundred% surge in overall money by yr two. Based on that foundation, RW, a experienced-operated company, was produced with goal of giving you alternative electrical remedies for a more intelligent and more sustainable future.

      Even more mainly, recognizing there is an untapped market in the market place and an improved approach to realize final results, RW is one of a handful of manufactures in the u.s. to place emphasis on individual acquire, concentrating in both industrial and home solar-powered town off-take. Its visualization is to make a purchases commercial infrastructure on a community-based, statewide, countrywide level, offering various sustainable fuel goods within the of RW.

      This enthusiasm in the actual sustainable industry goes on to stimulate and inspire Matthew in enduring his solicit to work with institutions that have the unchanging of offering limitless fuel tips for a a whole lot more ecological destiny. Matt has already a new in companies from Hesser College.

      Understanding why solar energy consultants assist clients via Matthew D’Agati.
      Sustainable Energy and Electricity Poverty: Bridging the Imbalance by matt d’agatiMatt D’Agati e3d74c8

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.