No need to go through it twice ... If the 'a' doesn't work and the string is more than one character, just make 'b' the last letter ...
@timc34063 жыл бұрын
Lol you are correct! Nice catch
@AM-wx9zl2 жыл бұрын
Doesn't work for "aba"
@icaryslittle63702 жыл бұрын
@@AM-wx9zl Yes, it does -- going through 'aba' once doesn't work, b/c 'aaa' is a palindrome, so if you make the last letter 'b', you get 'abb', which is the correct answer ...
@AM-wx9zl2 жыл бұрын
@@icaryslittle6370 you've already changed it to 'aaa' at that point, swapping the last letter to get 'aab' is the wrong answer. I may have misunderstood your original comment, do you have a code example? I couldn't get it to work but I'm not great at leetcode
@icaryslittle63702 жыл бұрын
Not that good *YET* ... Keep practicing, brother ... class Solution(object): def breakPalindrome(self, palindrome): if len(palindrome) == 1: return '' p = list(palindrome) for x in range(len(p)/2): temp = p[x] p[x] ='a' if p != p[::-1]: return ''.join(p) p[x] = temp p[-1] = 'b' if p != p[::-1]: return ''.join(p) return ''