From 7c60dee83b436e8e8a667bf9f191beced8de4fc3 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Mon, 10 Mar 2025 00:36:13 -0600 Subject: chore: move cpp and rust code to directory --- other/break_palindrom.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 other/break_palindrom.cpp (limited to 'other/break_palindrom.cpp') 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 +} -- cgit v1.2.3