aboutsummaryrefslogtreecommitdiff
path: root/other/break_palindrom.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/break_palindrom.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/break_palindrom.cpp')
-rw-r--r--other/break_palindrom.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/other/break_palindrom.cpp b/other/break_palindrom.cpp
new file mode 100644
index 0000000..d10c0e6
--- /dev/null
+++ b/other/break_palindrom.cpp
@@ -0,0 +1,33 @@
+// 1328. Break a Palindrome
+#include "leetcode.h"
+
+/*
+ * given a palindrome string of lowercase english letters 'palindrome' replace
+ * exactly one character with any lowercase english letter so that the resulting
+ * string is not a palindrome and that it is the lexicographically smallest one
+ * possible. return the resulting string. if there is no way to replace a
+ * character to make it not a palindrome, return an empty string.
+ */
+
+class Solution {
+public:
+ string breakPalindrome(string palindrome) {
+ int n = palindrome.size();
+ if (n == 1)
+ return "";
+ for (int i = 0; i < n / 2; i++) {
+ if (palindrome[i] != 'a') {
+ palindrome[i] = 'a';
+ return palindrome;
+ }
+ }
+ palindrome[n - 1] = 'b';
+ return palindrome;
+ }
+};
+
+int main() {
+ Solution obj;
+ cout << obj.breakPalindrome("abccba") << endl; // expect: aaccba
+ cout << obj.breakPalindrome("a") << endl; // expect: null
+}