Given a palindromic string palindrome, replace exactly one character by any lowercase English letter so that the string becomes the lexicographically smallest possible string that isn't a palindrome.
After doing so, return the final string. If there is no way to do so, return the empty string.
I have come up with the following, which takes O(n) time and O(1) space:
def breakPalindrome(self, S):
for i in xrange(len(S) / 2):
if S[i] != 'a':
return S[:i] + 'a' + S[i + 1:]
return S[:-1] + 'b' if S[:-1] else ''
Is there a better solution? Please correct me if there's some mistake in time or space complexity.