Simple C++ Solution || Runtime 6 ms Beats 16.14% Memory 6 MB
Intuition\nMy first Intuition was to find the inex of frst character using "Binary Search" and then replacing the next character in the original string with the
# Intuition\nMy first Intuition was to find the inex of frst character using "Binary Search" and then replacing the next character in the original string with the previous character. However it didn\'t proved to be an effective solution. So I came up with this second solution.\n\n# Approach\nIt is basically a greedy approach that checks:\n1 - If the length of string is greater than 2, it replaces the first character which is not equal to \'a\' with \'a\'. Example - "**abc**cba" here in the bold part, character at 1st index \'b\' != \'a\' so it replaces with \'a\'.\n2 - if length of string is 2, it replaces the last character with \'b\'.(The edge case \'bb\' will be handled in step 1!!!).\n## ***Hope you loved the explaination. If so please upvote my solution.*\n**\n# Complexity\n- Time complexity:\nTime Complexity is O(n/2) which is O(n);\n\n- Space complexity:\nSpace complexity is O(1). Since no extra space is used by us.\n\n# Code\n```\nclass Solution {\npublic:\n    string breakPalindrome(string str){\n        int len = str.size(), end = len / 2;\n        for (int i = 0; i < len/2; i++){\n            if(str[i] != \'a\'){\n                str[i] = \'a\';\n                return str;\n            }\n        }\n        if(len > 1){\n            str[len - 1] = \'b\';\n            return str;\n        }\n        return "";\n    }\n};\n```
['C++']