Javascript O(n) faster than 99.09%
Runtime: 57 ms, faster than 99.09% of JavaScript online submissions for Break a Palindrome. Memory Usage: 41.8 MB, less than 68.18% of JavaScript online submiss
Runtime: 57 ms, faster than 99.09% of JavaScript online submissions for Break a Palindrome. Memory Usage: 41.8 MB, less than 68.18% of JavaScript online submissions for Break a Palindrome.\n\nApproach : Changing the first alphabet that is not \'a\' to \'a\' making the string lexicographically the smallest one. Special case handling - In a case where all the character in the string are \'a\' then replacing the last \'a\' with \'b\' makes the best llexicographically smallest string.\n\n```\nvar breakPalindrome = function(palindrome) {\n    if(palindrome.length === 1){\n        return "";\n    }\n    let res = "";\n    for(let i=0; i < (Math.floor(palindrome.length / 2));i++){\n        if(palindrome[i] !== \'a\'){\n            res = palindrome.slice(0,i)  + \'a\' + palindrome.slice(i+1,palindrome.length);\n            break;\n        }\n    }\n    if(res === ""){\n        res = palindrome.slice(0,palindrome.length-1) + \'b\';\n    }\n    return res;\n};\n```
['Greedy', 'JavaScript']