Check if it is possible to create a palindrome string from given N | Java 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
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 " ) ; } }