Check if it is possible to create a palindrome string from given N
Last Updated :
24 Feb, 2023
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.
Approach:
- Obtain the substring corresponding to given number N and maintain its digit’s sum.
- Append the substring till its length becomes equal to the sum of digits of N.
- Check if the string obtained is Palindrome or not.
- If it is a Palindrome, print YES.
- Else, print NO.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
bool isPalindrome(string s)
{
string s1 = "" ;
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 letters = "abcdefghij" ;
int sum = 0;
string substr = "" ;
for ( int i = 0; i < s.length(); i++)
{
int digit = s[i] - '0' ;
substr += letters[digit];
sum += digit;
}
while (str.length() <= sum)
{
str += substr;
}
str = str.substr(0, sum);
return isPalindrome(str);
}
int main()
{
int N = 61;
bool flag = createString(N);
if (flag)
cout << "YES" ;
else
cout << "NO" ;
}
|
Java
import java.io.*;
import java.util.*;
public class GFG {
static boolean isPalindrome(String s)
{
String s1 = "" ;
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 letters = "abcdefghij" ;
int sum = 0 ;
String substr = "" ;
for ( int i = 0 ; i < s.length(); i++) {
int digit = s.charAt(i) - '0' ;
substr += letters.charAt(digit);
sum += digit;
}
while (str.length() <= sum) {
str += substr;
}
str = str.substring( 0 , sum);
return isPalindrome(str);
}
public static void main(String args[])
{
int N = 61 ;
boolean flag = createString(N);
if (flag)
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
def isPalindrome(s):
s1 = ""
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)
letters = "abcdefghij"
sum = 0
substr = ""
for i in range ( 0 , len (s)) :
digit = int (s[i])
substr + = letters[digit]
sum + = digit
while ( len (s2) < = sum ):
s2 + = substr
s2 = s2[: sum ]
return isPalindrome(s2)
N = 61 ;
flag = createString(N)
if (flag):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
static bool isPalindrome(String s)
{
String s1 = "" ;
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 letters = "abcdefghij" ;
int sum = 0;
String substr = "" ;
for ( int i = 0; i < s.Length; i++)
{
int digit = s[i] - '0' ;
substr += letters[digit];
sum += digit;
}
while (str.Length <= sum)
{
str += substr;
}
str = str.Substring(0, sum);
return isPalindrome(str);
}
public static void Main()
{
int N = 61;
bool flag = createString(N);
if (flag)
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
Javascript
function isPalindrome(s) {
let s1 = "" ;
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();
let letters = "abcdefghij" ;
let sum = 0;
let substr = "" ;
for (let i = 0; i < s.length; i++) {
let digit = parseInt(s[i]);
substr += letters[digit];
sum += digit;
}
while (s2.length <= sum) {
s2 += substr;
}
s2 = s2.substring(0, sum);
return isPalindrome(s2);
}
let N = 61;
let flag = createString(N);
if (flag) {
console.log( "YES" );
} else {
console.log( "NO" );
}
|
Share your thoughts in the comments
Please Login to comment...