Skip to content

Commit 432d071

Browse files
authored
Create break-a-palindrome.cpp
1 parent efb2f51 commit 432d071

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

C++/break-a-palindrome.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
string breakPalindrome(string palindrome) {
7+
for (int i = 0; i < palindrome.length() / 2; ++i) {
8+
if (palindrome[i] != 'a') {
9+
palindrome[i] = 'a';
10+
return palindrome;
11+
}
12+
}
13+
if (palindrome.length() >= 2) {
14+
palindrome.back() = 'b';
15+
return palindrome;
16+
}
17+
return "";
18+
}
19+
};

0 commit comments

Comments
 (0)