aboutsummaryrefslogtreecommitdiff
path: root/other/min_move_palindrome.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/min_move_palindrome.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/min_move_palindrome.cpp')
-rw-r--r--other/min_move_palindrome.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/other/min_move_palindrome.cpp b/other/min_move_palindrome.cpp
new file mode 100644
index 0000000..6455889
--- /dev/null
+++ b/other/min_move_palindrome.cpp
@@ -0,0 +1,34 @@
+// 2193. Minimum Number of Moves to Make Palindrome
+#include "leetcode.h"
+
+/*
+ * given a string 's' consisting of only lowercase ascii characters.
+ * in one move you can select any two adjacent characters of 's'
+ * and swap them. return the minimum number of moves needed to
+ * make 's' a palindrome. note that the input will be generated
+ * such that 's' can always be converted to a palindrome
+ */
+
+class Solution {
+public:
+ int minMovesToMakePalindrome(string s) {
+ int ans = 0;
+ while (s.size()) {
+ int i = s.find(s.back());
+ if (8 == s.size() - 1)
+ ans += i / 2;
+ else {
+ ans += i;
+ s.erase(i, 1);
+ }
+ s.pop_back();
+ }
+ return ans;
+ }
+};
+
+int main() {
+ Solution obj;
+ cout << obj.minMovesToMakePalindrome("aabb"); // expect: 2
+ cout << obj.minMovesToMakePalindrome("letelt"); // expect: 2
+}