Tuesday, 3 May 2016

EXCEL SHEET NAMING ALGO

MS Excel columns has a pattern like A, B, C, … ,Z, AA, AB, AC,…. ,AZ, BA, BB, … ZZ, AAA, AAB ….. etc. In other words, column 1 is named as “A”, column 2 as “B”, column 27 as “AA”.
Given a column number, find its corresponding Excel column name. Following are more examples.
Input          Output
 26             Z
 51             AY
 52             AZ
 80             CB
 676            YZ
 702            ZZ
 705            AAC


-----------------------------------EDITORIAL----------------------------------------------------------
THIS PROBLEM CAN BE SOLVED IN MANY MAYS , 
I SOLVED THIS IN 2 WAYS 
1ST APPROACH 
FIRST FIND LENGTH OF THE LONGEST STRING THAT  CAN BE THE ANSWER OF THE GIVEN NUMBER 

NOW DECREASE THE NUM BY ALL POSSIBLE STRING OF LEN-1,

NOW PROBLEM REDUCE TO ,  WE HAVE TO FIND NTH ( REMAINING N )   PERMUTATION OF THE STRING OF LENGTH LEN , ( WE HAVE AMPLE NUMBER OF EACH CHARACTER ) 

THIS PROBLEM CAN EASILY SOLVE BY 1 BY FIXING CHARACTERS  STARTING FROM 
THE MSB POSITION ( THIS PROBLEM  IS SIMILAR TO MY LAST POST  ONLY DIFFERENCE   IS THAT HERE WE HAVE  NO RESTRICTION ON THE NUMBER OF CHARACTERS )

-------------------------------------------CODE-------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
  lli len=0;
  lli fact[11];
  lli solve(lli cur_pos,lli rem)
   {
    if(cur_pos>len)
     {
      cout<<endl;
      return 0;
}
else
{
lli i=0;
for(i=0;i<26;i++)
  {
   lli  cov=fact[len-cur_pos];
   if(cov>=rem) break;
   else
   rem-=cov;
   
  }
  printf("%c",(char)(i+'a'));
  solve(cur_pos+1,rem);
}
   }
int main()
 {
  lli t;
  cin>>t;
 
  fact[0]=1;
  for(lli i=1;i<=10;i++) fact[i]=fact[i-1]*26;
   
  while(t--)
   {
    lli n;
     cin>>n;
    len=0;
     lli sum=0;
     lli prev=1;
     for(lli i=0;i<10;i++)
      {
      sum+=prev*26;
      prev*=26;
      len++;
      if(sum>=n) break;
     
  }
  for(int i=1;i<=len-1;i++) n-=fact[i];
    //n-=fact[len-1];
    //cout<<" reduce n "<<n<<endl;
 solve(1,n);
  }
   
 }



2ND  EASY APPROACH  # OBSERVATION 
Suppose we have a number n, let’s say 28. so corresponding to it we need to print the column name. We need to take remainder with 26.
If remainder with 26 comes out to be 0 (meaning 26, 52 and so on) then we put ‘Z’ in the output string and new n becomes n/26 -1 because here we are considering 26 to be ‘Z’ while in actual it’s 25th with respect to ‘A’.
Similarly if the remainder comes out to be non zero. (like 1, 2, 3 and so on) then we need to just insert the char accordingly in the string and do n = n/26.
Finally we reverse the string and print.
Example:
n = 700
Remainder (n%26) is 24. So we put ‘X’ in output string and n becomes n/26 which is 26.
Remainder (26%26) is 0. So we put ‘Z’ in output string and n becomes n/26 -1 which is 0.
Following is C++ implementation of above approach.
#include<iostream>
#include<cstring>
#define MAX 50
using namespace std;
 
// Function to print Excel column name for a given column number
void printString(int n)
{
    char str[MAX];  // To store result (Excel column name)
    int i = 0;  // To store current index in str which is result
 
    while (n>0)
    {
        // Find remainder
        int rem = n%26;
 
        // If remainder is 0, then a 'Z' must be there in output
        if (rem==0)
        {
            str[i++] = 'Z';
            n = (n/26)-1;
        }
        else // If remainder is non-zero
        {
            str[i++] = (rem-1) + 'A';
            n = n/26;
        }
    }
    str[i] = '\0';
 
    // Reverse the string and print result
    cout << strrev(str) << endl;
 
    return;
}
 
// Driver program to test above function
int main()
{
    printString(26);
    printString(51);
    printString(52);
    printString(80);
    printString(676);
    printString(702);
    printString(705);
    return 0;
}

No comments:

Post a Comment