Break a Palindrome
https://leetcode.com/problems/break-a-palindrome/
class Solution {
public:
string breakPalindrome(string palindrome) {
int l=palindrome.size();
if(l==1) return "";
for(int i=0;i<l/2;i++){ //Iterating over first half only as other half will be same bcz of PALINDROME
if(palindrome[i]!='a'){
palindrome[i]='a'; //Replace first non -'a' with 'a'
return palindrome;
}
}
palindrome[l-1]='b'; //If all a's then just replace last element with 'b'
return palindrome;
}
};
Comments
Post a Comment