Palindromic Series | Practice | GeeksforGeeks

Adobe wants to play a game. He is given a number N. He has to create a alphabetical string in lower case from that number and tell whether the string is palindrome or not. a = 0 , b = 1..... and so on.  For eg : If the number is 61 the substring “gb” will be printed till 7 (6+1) characters i.e. “gbgbgbg” and check if palindrome or not. Adobe is weak in concepts of palindrome and strings, help him in winning the game.
Note: No number will start with zero. Consider alphabets ' a to j ' only i.e. single digit numbers from 0 to 9.
Example 1:
â€‹Input : N = 61
Output : YES
Explanation:
N = 61 the substring “gb” will be 
printed till 7 (6+1) characters i.e. 
“gbgbgbg” and it is palindrome. return 
true.


â€‹Example 2:
Input : N = 1998 
Output :  NO 
 
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function pallan() that takes an integer (N), and return true if the string is a palindrome otherwise return false if not. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).

Constraints:
1 ≤ N ≤ 107
0
mayank20212 months ago
C++bool pallan(int n1){      int sum=0, n=n1;   while(n)   {       sum=sum+n%10;       n=n/10;   }   string n11=to_string(n1), n12;   for(int i=0; i<n11.size(); i++)    {        n11[i]=n11[i]-'0'+'a';            }   while(sum >= n11.size())    {                n12=n12+n11;        sum=sum-n11.size();    }    if(sum)    n12=n12+n11.substr(0,sum);         int i=0, j=n12.size()-1;   while(i<j)     {         if(n12[i] != n12[j])          return 0;          i++; j--;     }      return 1;   }
0
jainpari25092 months ago
bool pallan(int n){   int sum=0;   string s="";   if(n<11)   return true;   while(n!=0)   {       int ch=n%10;       switch(ch)       {           case 0:           s='a'+s;           break;           case 1:           s='b'+s;           break;           case 2:           s='c'+s;           break;           case 3:           s='d'+s;           break;           case 4:           s='e'+s;           break;           case 5:           s='f'+s;           break;           case 6:           s='g'+s;           break;           case 7:           s='h'+s;           break;           case 8:           s='i'+s;           break;           case 9:           s='j'+s;           break;       }       n/=10;       sum+=ch;   }   int r=s.length();   for(int i=0;i<sum-r;i++)   s+=s[i];   for(int i=0,j=sum-1;i<sum/2;i++,j--)   {       if(s[i]!=s[j])       return false;   }   return true;   }
0
amiransarimy3 months ago
Simple Python Solutions
Total Time Taken: 0.0/8.9

def pallan (n) : 
   mdict ={0:'a',1:'b',2:'c',3:'d',4:'e',5:'f',6:'g',7:'h',8:'i',9:'j'}
   nstr = str(n) #convert integer into string 
   res ="" #resulttant string
   mcount=0
   total_length = 0
   for num in nstr:
       int_n = int(num)
       res += mdict[int_n]
       total_length += int_n
   
   mcount = total_length//len(res)
   mres = res*mcount
   mres += res[0:total_length-len(mres)]
   rev_mres = mres[::-1] #reverse string
   return True if mres == rev_mres else False
-7
chessnoobdj3 months ago
C++
bool pallan(int n){
    string s = "";
    int sum = 0;
    while(n){
        sum += n%10;
        s += 'a'+(n%10);
        n /= 10;
    }
    reverse(s.begin(), s.end());
    int len = s.size();
    string str = s;
    for(int i=0; i<(sum/len)-1; i++){
        s += str;
    }
    s += s.substr(0, sum%len);
    int l = 0, r = s.size()-1;
    while(l <= r){
        if(s[l] != s[r])
            return false;
        l += 1;
        r -= 1;
    }
    return true;
}
+2
hemantsoni4 months ago
bool pallan(int n){
    
    unordered_map <int , char>mp ={{'0','a'},{'1','b'},{'2','c'},{'3','d'},{'4','e'},
                                    {'5','f'},{'6','g'},{'7','h'},{'8','i'},{'9','j'}};
    
    int size=0;
    int k=n;
    int count =0;
    string s="";
         while(k){
            size += k%10;
            k= k/10;
            count++;
        }
    int lot = size/count;
    int rem = size%count;
    
    string num = to_string(n);
    
    while(lot--){
        for(int i =0; i<num.size();++i){
            s +=mp[num[i]];
        }
    }
    
    for(int i =0; i<rem;++i){
        s+=mp[num[i]];
    }
    
    //for palandromic check
    int i = 0;
    int j = size-1;
    while(i<=j){
        if(s[i]!=s[j]) return false;
            i++;
            j--;

    }
    return true;
}
0
Priyam Saxena10 months ago
Priyam Saxena
class Complete{
    // Function for finding maximum and value pair    public static boolean pallan (int n) {        int l = 0;        char ar[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n',        'o','p','q','r','s','t','u','v','w','x','y','z'};        String s = "";        while(n!=0)        {            int a = n%10;            s = String.valueOf(ar[a]) + s;            l+=a;            n/=10;        }        if(l<s.length()) {="" return="" pal(s.substring(0,l));="" }="" string="" t="s;" int="" m="s.length();" while(m<l)="" {="" for(int="" i="0;i&lt;s.length();i++)" {="" t="t" +="" string.valueof(s.charat(i));="" m++;="" if(m="=l)" {="" break;="" }="" }="" }="" return="" pal(t);="" }="" static="" boolean="" pal(string="" s)="" {="" int="" i="0;" int="" j="s.length()-1;" while(i<j)="" {="" if(s.charat(i)!="s.charAt(j))" {="" return="" false;="" }="" i++;="" j--;="" }="" return="" true;="" }="" }="">
0
abhikarsh gupta1 year ago
abhikarsh gupta
Time complexity - O(n), space - O(n).. using unordered_map, auxillary strings.. looks long. but is very easy to understand
bool pallan(int n){     // Complete the function    unordered_map<int,char> mp;    mp.insert({0,'a'});    mp.insert({1,'b'});    mp.insert({2,'c'});    mp.insert({3,'d'});    mp.insert({4,'e'});    mp.insert({5,'f'});    mp.insert({6,'g'});    mp.insert({7,'h'});    mp.insert({8,'i'});    mp.insert({9,'j'});     string temp = "";    int sum = 0;    // calculate sum of digits of number and create a string of number-values    while( n > 0)    {        sum += n%10;        temp += mp[(n %10)];        n /= 10;    }    // the string is generated in reverse at start    reverse(temp.begin(), temp.end());    int len = temp.size();    string s = temp;    // number is 10 -> "ba" which will only consider - "b"..    if(sum < len)    {        int num = sum % len;        // need to regenerate string from scratch as it will be shorter.        temp = "";        for(int i = 0; i < num; i++)            temp += s[i];    }    // when sum is greater than string size    else    {   sum -= len;        while(sum > len)        {            temp += s;            sum -= len;        }        int num = sum % len;        for(int i = 0; i < num; i++)            temp += s[i];    }     string res = temp;    // check palindrome     reverse(temp.begin(), temp.end());    for(int i = 0; i < temp.size(); i++)        if(res[i] != temp[i])            return false;    return true;}
0
asifalimnstu2 years ago
asifalimnstu
0.01 sechttps://practice.geeksforge...
0
Rahul Kumar Vairagade2 years ago
Rahul Kumar Vairagade
C++ solutionTime Complexity: 0.01https://ide.geeksforgeeks.o...
0
Md Shahfahad2 years ago
Md Shahfahad
easy python sol..0.03 sec
https://ide.geeksforgeeks.o...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.