sorry, in the for loop we only need to iterate only halfway through the string. The corrected code is as follows: string breakPalindrome(string& palindromeStr) { int n = palindromeStr.size(); // base case if(n == 1) return "IMPOSSIBLE"; for(int i = 0; i < n/2; i++) { if(palindromeStr[i] != 'a') { palindromeStr[i] = 'a'; return palindromeStr; } } return "IMPOSSIBLE"; }
@@Leetcore-2508 the updated loop also incorrect, if we have "aaaabaaaa", we can change the latest character to b, so the string is "aaaabaaab", I think that is the answer, but if we run the code then it will have answer of impossible
is it possible to be the same question for SWE intern now ?
@@Haifajundi no
If the palindrome is "aba" then after replacing 'b' with 'a' it will be still a palindrome "aaa"
sorry, in the for loop we only need to iterate only halfway through the string.
The corrected code is as follows:
string breakPalindrome(string& palindromeStr) {
int n = palindromeStr.size();
// base case
if(n == 1)
return "IMPOSSIBLE";
for(int i = 0; i < n/2; i++) {
if(palindromeStr[i] != 'a') {
palindromeStr[i] = 'a';
return palindromeStr;
}
}
return "IMPOSSIBLE";
}
@@Leetcore-2508 the updated loop also incorrect, if we have "aaaabaaaa", we can change the latest character to b, so the string is "aaaabaaab", I think that is the answer, but if we run the code then it will have answer of impossible
How many days there reply comes back
@@vedangjadhav3036 atleast 2 weeks.
Link to the document: drive.google.com/file/d/1xCSRbO95xxq5KJE8cVoK7iTskxnZqbBw/view?usp=sharing
Join my Telegram Group for Coding and Interview Discussions: t.me/+P87TYGAe7-w2ZjU1