In this challenge, you will be given a palindrome which you must modify 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 two conditions:
• The new string is not a palindrome
• The new string is lower lexicographically (alphabetically) than the initial string.
• The new string is the lowest value string lexicographically that can be created from the original palindrome after making only one change.
If it not possible to create a string meeting the criteria, return the string IMPOSSIBLE.
Function Description
Complete the function breakPalindrome in the editor below. The function must return the resulting string or IMPOSSIBLE if one cannot be formed.
breakPalindrome has the following parameter(s):
s: the original string
Sample Case
Sample Input For Custom Testing
bab
Sample Output
aab
Explanation The string aab is the smallest string which satisfies all the given conditions.
题意:
给定?一个palindrome。要改变?一个char,使得新的string不不是palindrome。?而且要是lexicocographically最?小的,不不然就返回"IMPOSSIBLE"。
把左半边不不是a的改成a就?行行了了。举例例:输?aba。算法算出aaa但仍是回?文字串串,所以输出IMPOSSIBLE。 输?入abba,输出aaba。
思路:
代码:
原文地址:https://www.cnblogs.com/liuliu5151/p/11509825.html