In Java please:
Palindrome reads the same from left or right, mom for example. There is a palindrome which must be modified, if possible. Change exactly one character of the string to another character in the range ascii[a-z] so that the string meets the following three conditions:
a. The new string is lower alphabetically than the initial string.
b. The new string is the lowest value string alphabetically that can be created from the original palindrome after making only one change.
c. The new string is not a palindrome.
Return the new string, or, if it not possible to create a string meeting the criteria, return the string IMPOSSIBLE.
Example:
palindromeStr = 'aaabbaaa'
O Possible strings lower alphabetically than 'aaabbaaa' after one change are [aaaabaaa', 'aaabaaaa').
O aaaabaaa' is not a palindrome and is the lowest string that can be created from palindromeStr.

Respuesta :

Answer:

See explaination

Explanation:

public class PalindromeChange {

public static void main(String[] args) {

System.out.println("mom to non palindrom word is: "+changTONonPalindrom("mom"));

System.out.println("mom to non palindrom word is: "+changTONonPalindrom("aaabbaaa"));

}

private static String changTONonPalindrom(String str)

{

int mid=str.length()/2;

boolean found=false;

char character=' ';

int i;

for(i=mid-1;i>=0;i--)

{

character = str.charAt(i);

if(character!='a')

{

found=true;

break;

}

}

if(!found)

{

for(i=mid+1;i<str.length();i++)

{

character = str.charAt(i);

if(character!='a')

{

found=true;

break;

}

}

}

// This gives the character 'a'

int ascii = (int) character;

ascii-=1;

str = str.substring(0, i) + (char)ascii+ str.substring(i + 1);

return str;

}

}