Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on String
  • Practice String
  • MCQs on String
  • Tutorial on String
  • String Operations
  • Sort String
  • Substring & Subsequence
  • Iterate String
  • Reverse String
  • Rotate String
  • String Concatenation
  • Compare Strings
  • KMP Algorithm
  • Boyer-Moore Algorithm
  • Rabin-Karp Algorithm
  • Z Algorithm
  • String Guide for CP
Open In App
Next Article:
Check if a given string is a rotation of a palindrome
Next article icon

Check if it is possible to create a palindrome string from given N

Last Updated : 24 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a number N. The task is to create an 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. 

Note: No number will start with zero. Consider alphabets ‘ a to j ‘ only i.e. single digit numbers from 0 to 9. 

Examples:

Input: N = 61 
Output: YES 
Numbers 6, 1 represent letters ‘g’, ‘b’ respectively. So the substring is ‘gb’ and the sum is 7(6+1). Thus the alphabetical string formed is ‘gbgbgbg’, and is a palindrome. 

Input: N = 1998 
Output: NO 
Numbers 1, 9, 8 represent letters ‘b’, ‘j’ and ‘i’ respectively. So the substring is ‘bjji’ and sum is 27(1+9+9+8). Thus the alphabetical string formed is bjjibjjibjjibjjibjjibjjibjj’, and is not a palindrome.

Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

Approach:

  1. Obtain the substring corresponding to given number N and maintain its digit’s sum.
  2. Append the substring till its length becomes equal to the sum of digits of N.
  3. Check if the string obtained is Palindrome or not.
  4. If it is a Palindrome, print YES.
  5. Else, print NO.

Below is the implementation of the above approach: 

C++




// C++ implementation of the
// above approach
#include<bits/stdc++.h>
using namespace std;
 
// Function to check if a string
// is palindrome or not
bool isPalindrome(string s)
{
    // String that stores characters
    // of s in reverse order
    string s1 = "";
 
    // Length of the string s
    int N = s.length();
 
    for (int i = N - 1; i >= 0; i--)
        s1 += s[i];
 
    if (s == s1)
        return true;
    return false;
}
 
bool createString(int N)
{
    string str = "";
    string s = to_string(N);
 
    // String used to form substring
    // using N
    string letters = "abcdefghij";
     
    // Variable to store sum
    // of digits of N
    int sum = 0;
    string substr = "";
 
    // Forming the substring
    // by traversing N
    for (int i = 0; i < s.length(); i++)
    {
        int digit = s[i] - '0';
        substr += letters[digit];
        sum += digit;
    }
 
    // Appending the substr to str till
    // it's length becomes equal to sum
    while (str.length() <= sum)
    {
        str += substr;
    }
 
    // Trimming the string str so that
    // it's length becomes equal to sum
    str = str.substr(0, sum);
 
    return isPalindrome(str);
}
 
// Driver code
int main()
{
    int N = 61;
 
    // Calling function isPalindrome to
    // check if str is Palindrome or not
    bool flag = createString(N);
    if (flag)
        cout << "YES";
    else
        cout << "NO";
}
 
// This code is contributed by ihritik
 
 

Java




// Java implementation of the above approach
import java.io.*;
import java.util.*;
 
public class GFG {
 
    // Function to check if a string is palindrome or not
    static boolean isPalindrome(String s)
    {
        // String that stores characters
        // of s in reverse order
        String s1 = "";
 
        // Length of the string s
        int N = s.length();
 
        for (int i = N - 1; i >= 0; i--)
            s1 += s.charAt(i);
 
        if (s.equals(s1))
            return true;
        return false;
    }
 
    static boolean createString(int N)
    {
        String str = "";
        String s = "" + N;
 
        // String used to form substring using N
        String letters = "abcdefghij";
        // Variable to store sum of digits of N
        int sum = 0;
        String substr = "";
 
        // Forming the substring by traversing N
        for (int i = 0; i < s.length(); i++) {
            int digit = s.charAt(i) - '0';
            substr += letters.charAt(digit);
            sum += digit;
        }
 
        // Appending the substr to str
        // till it's length becomes equal to sum
        while (str.length() <= sum) {
            str += substr;
        }
 
        // Trimming the string str so that
        // it's length becomes equal to sum
        str = str.substring(0, sum);
 
        return isPalindrome(str);
    }
 
    // Driver code
    public static void main(String args[])
    {
        int N = 61;
 
        // Calling function isPalindrome to
        // check if str is Palindrome or not
        boolean flag = createString(N);
        if (flag)
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}
 
 

Python3




# Python3 implementation of
# the above approach
 
# Function to check if a string
# is palindrome or not
def isPalindrome(s):
 
    # String that stores characters
    # of s in reverse order
    s1 = ""
 
    # Length of the string s
    N = len(s)
    i = (N - 1)
    while(i >= 0):
        s1 += s[i]
        i = i - 1
 
    if (s == s1):
        return True
    return False
 
def createString(N):
 
    s2 = ""
    s = str(N)
 
    # String used to form
    # substring using N
    letters = "abcdefghij"
     
    # Variable to store sum
    # of digits of N
    sum = 0
    substr = ""
 
    # Forming the substring
    # by traversing N
    for i in range(0, len(s)) :
        digit = int(s[i])
        substr += letters[digit]
        sum += digit
     
    # Appending the substr to str till
    # it's length becomes equal to sum
    while (len(s2) <= sum):
        s2 += substr
 
    # Trimming the string str so that
    # it's length becomes equal to sum
    s2 = s2[:sum]
 
    return isPalindrome(s2)
 
# Driver code
N = 61;
 
# Calling function isPalindrome to
# check if str is Palindrome or not
flag = createString(N)
if (flag):
    print("YES")
else:
    print("NO")
 
# This code is contributed by ihritik
 
 

C#




// C# implementation of the
// above approach
using System;
 
class GFG
{
 
// Function to check if a string
// is palindrome or not
static bool isPalindrome(String s)
{
    // String that stores characters
    // of s in reverse order
    String s1 = "";
 
    // Length of the string s
    int N = s.Length;
 
    for (int i = N - 1; i >= 0; i--)
        s1 += s[i];
 
    if (s.Equals(s1))
        return true;
    return false;
}
 
static bool createString(int N)
{
    String str = "";
    String s = "" + N;
 
    // String used to form substring
    // using N
    String letters = "abcdefghij";
     
    // Variable to store sum
    // of digits of N
    int sum = 0;
    String substr = "";
 
    // Forming the substring
    // by traversing N
    for (int i = 0; i < s.Length; i++)
    {
        int digit = s[i] - '0';
        substr += letters[digit];
        sum += digit;
    }
 
    // Appending the substr to str till
    // it's length becomes equal to sum
    while (str.Length <= sum)
    {
        str += substr;
    }
 
    // Trimming the string str so that
    // it's length becomes equal to sum
    str = str.Substring(0, sum);
 
    return isPalindrome(str);
}
 
// Driver code
public static void Main()
{
    int N = 61;
 
    // Calling function isPalindrome to
    // check if str is Palindrome or not
    bool flag = createString(N);
    if (flag)
        Console.WriteLine("YES");
    else
        Console.WriteLine("NO");
}
}
 
// This code is contributed
// by ihritik
 
 

Javascript




// JavaScript implementation of the above approach
 
// Function to check if a string
// is palindrome or not
function isPalindrome(s) {
 
// String that stores characters
// of s in reverse order
let s1 = "";
 
// Length of the string s
let N = s.length;
let i = (N - 1);
while (i >= 0) {
    s1 += s[i];
    i = i - 1;
}
 
if (s == s1) {
    return true;
}
    return false;
}
 
function createString(N) {
 
let s2 = "";
let s = N.toString();
 
// String used to form
// substring using N
let letters = "abcdefghij";
 
// Variable to store sum
// of digits of N
let sum = 0;
let substr = "";
 
// Forming the substring
// by traversing N
for (let i = 0; i < s.length; i++) {
    let digit = parseInt(s[i]);
    substr += letters[digit];
    sum += digit;
}
 
// Appending the substr to str till
// it's length becomes equal to sum
while (s2.length <= sum) {
    s2 += substr;
}
 
// Trimming the string str so that
// it's length becomes equal to sum
s2 = s2.substring(0, sum);
 
return isPalindrome(s2);
}
 
// Driver code
let N = 61;
 
// Calling function isPalindrome to
// check if str is Palindrome or not
let flag = createString(N);
if (flag) {
    console.log("YES");
} else {
    console.log("NO");
}
 
// This code is contributed by codebraxnzt
 
 
Output
YES


Next Article
Check if a given string is a rotation of a palindrome
author
rachana soma
Improve
Article Tags :
  • DSA
  • Strings
  • Arrays
  • palindrome
Practice Tags :
  • Arrays
  • palindrome
  • Strings

Similar Reads

  • Recursive function to check if a string is palindrome
    Given a string s, the task is to check if it is a palindrome or not. Examples: Input: s = "abba"Output: YesExplanation: s is a palindrome Input: s = "abc" Output: NoExplanation: s is not a palindrome Using Recursion and Two Pointers - O(n) time and O(n) spaceThe idea is to recursively check if the s
    8 min read
  • Check if a given string is Even-Odd Palindrome or not
    Given a string str, the task is to check if the given string is Even-Odd Palindrome or not. An Even-Odd Palindrome string is defined to be a string whose characters at even indices form a Palindrome while the characters at odd indices also form a Palindrome separately. Examples: Input: str="abzzab"
    7 min read
  • Check if K palindromic strings can be formed from a given string
    Given a string S of size N and an integer K, the task is to find whether the characters of the string can be arranged to make K palindromic strings simultaneously. Examples: Input: S = "annabelle", K = 2 Output: Yes Explanation: All characters of string S can be distributed into "elble" and "anna" w
    7 min read
  • Check if a given string is a rotation of a palindrome
    Given a string, check if it is a rotation of a palindrome. For example your function should return true for "aab" as it is a rotation of "aba". Examples: Input: str = "aaaad" Output: 1 // "aaaad" is a rotation of a palindrome "aadaa" Input: str = "abcd" Output: 0 // "abcd" is not a rotation of any p
    15+ min read
  • Rearrange characters to form palindrome if possible
    Given a string, convert the string to palindrome without any modifications like adding a character, removing a character, replacing a character etc. Examples: Input : "mdaam" Output : "madam" or "amdma" Input : "abb" Output : "bab" Input : "geeksforgeeks" Output : "No Palindrome"Count occurrences of
    7 min read
  • Longest palindromic string possible by concatenating strings from a given array
    Given an array of strings S[] consisting of N distinct strings of length M. The task is to generate the longest possible palindromic string by concatenating some strings from the given array. Examples: Input: N = 4, M = 3, S[] = {"omg", "bbb", "ffd", "gmo"}Output: omgbbbgmoExplanation: Strings "omg"
    8 min read
  • Check given string is oddly palindrome or not | Set 2
    Given string str, the task is to check if characters at the odd indexes of str form a palindrome string or not. If not then print "No" else print "Yes". Examples: Input: str = "osafdfgsg", N = 9 Output: Yes Explanation: Odd indexed characters are = { s, f, f, s } so it will make palindromic string,
    12 min read
  • Remove a character from a string to make it a palindrome
    Given a string, we need to check whether it is possible to make this string a palindrome after removing exactly one character from this. Examples: Input : str = “abcba” Output : Yes we can remove character ‘c’ to make string palindrome Input : str = “abcbea” Output : Yes we can remove character ‘e’
    10 min read
  • Check if concatenation of splitted substrings of two given strings forms a palindrome or not
    Given two strings a and b of the same length, the task is to check if splitting both the strings and concatenating their opposite substrings, i.e. concatenating the left substring of a with right substring of b or concatenating the left substring of b with right substring of a, forms a palindrome or
    8 min read
  • How to check whether a passed string is palindrome or not in JavaScript?
    We are given a string, our task is to find string is palindrome or not. A palindrome is a series of numbers, strings, or letters that, when read from right to left and left to right, match each other exactly or produce the same series of characters. in simple words when number strings or characters
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences