Skip to content

Commit 7419a71

Browse files
authored
Create 2193-minimum-number-of-moves-to-make-palindrome.js
1 parent 44241bc commit 7419a71

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const minMovesToMakePalindrome = function(s) {
6+
let res = 0
7+
const arr = s.split('')
8+
9+
while(arr.length) {
10+
const idx = arr.indexOf(arr[arr.length - 1])
11+
if(idx === arr.length - 1) {
12+
res += ~~(idx / 2)
13+
} else {
14+
res += idx
15+
arr.splice(idx, 1)
16+
}
17+
arr.pop()
18+
}
19+
20+
return res
21+
};

0 commit comments

Comments
 (0)