Check if it is possible to create a palindrome string from given N | C ++ implementation of the above approach ; Function to check if a string is palindrome or not ; String that stores characters of s in reverse order ; Length of the string s ; String used to form substring using N ; Variable to store sum of digits of N ; Forming the substring by traversing N ; Appending the substr to str till it 's length becomes equal to sum ; Trimming the string str so that it 's length becomes equal to sum ; Driver code ; Calling function isPalindrome to check if str is Palindrome or not
#include <bits/stdc++.h> NEW_LINE 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 " ; }