aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--break_palindrom.c27
-rw-r--r--break_palindrom.cpp14
-rw-r--r--break_palindrom.py27
3 files changed, 66 insertions, 2 deletions
diff --git a/break_palindrom.c b/break_palindrom.c
new file mode 100644
index 0000000..2a1f428
--- /dev/null
+++ b/break_palindrom.c
@@ -0,0 +1,27 @@
+// 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.
+ */
+
+char *breakPalindrome(char *palindrome) {
+ int n = strlen(palindrome);
+ for (int i = 0; i < n / 2; ++i)
+ if (palindrome[i] != 'a') {
+ palindrome[i] = 'a';
+ return palindrome;
+ }
+ palindrome[n - 1] = 'b';
+ return n < 2 ? "" : palindrome;
+}
+
+int main() {
+ char p1[] = "abccba", p2[] = "a";
+ printf("%s\n", breakPalindrome(p1)); // expect: aaccba
+ printf("%s\n", breakPalindrome(p2)); // expect: null
+}
diff --git a/break_palindrom.cpp b/break_palindrom.cpp
index 579359a..d10c0e6 100644
--- a/break_palindrom.cpp
+++ b/break_palindrom.cpp
@@ -1,8 +1,17 @@
+// 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 breakPalindrom(string palindrome) {
+ string breakPalindrome(string palindrome) {
int n = palindrome.size();
if (n == 1)
return "";
@@ -19,5 +28,6 @@ public:
int main() {
Solution obj;
- cout << obj.breakPalindrom("abccba");
+ cout << obj.breakPalindrome("abccba") << endl; // expect: aaccba
+ cout << obj.breakPalindrome("a") << endl; // expect: null
}
diff --git a/break_palindrom.py b/break_palindrom.py
new file mode 100644
index 0000000..b83b295
--- /dev/null
+++ b/break_palindrom.py
@@ -0,0 +1,27 @@
+# 1328. Break a Palindrome
+
+"""
+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(object):
+ def breakPalindrome(self, palindrome):
+ """
+ :type palindrome: str
+ :rtype: str
+ """
+ for i in range(len(palindrome) / 2):
+ if palindrome[i] != "a":
+ return palindrome[:i] + "a" + palindrome[i + 1 :]
+ return palindrome[:-1] + "b" if palindrome[:-1] else ""
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.breakPalindrome(palindrome="abccba"))
+ print(obj.breakPalindrome(palindrome="a"))