diff options
Diffstat (limited to 'min_move_palindrome.cpp')
-rw-r--r-- | min_move_palindrome.cpp | 34 |
1 files changed, 0 insertions, 34 deletions
diff --git a/min_move_palindrome.cpp b/min_move_palindrome.cpp deleted file mode 100644 index 6455889..0000000 --- a/min_move_palindrome.cpp +++ /dev/null @@ -1,34 +0,0 @@ -// 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 -} |