Skip to content

Commit 03ada05

Browse files
committed
Still working on min adjacent swap.
1 parent 8214038 commit 03ada05

File tree

1 file changed

+56
-36
lines changed

1 file changed

+56
-36
lines changed

palindrome/min-adjacent-swap.js

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,77 @@
33
* @date: 29/01/2020
44
*/
55

6-
const isPalindrome = (str) => {
7-
const reversedStr = str.split('').reverse().join('');
8-
console.log('Resversed', reversedStr);
9-
if (str === reversedStr) {
10-
return true;
11-
}
12-
return false;
6+
const isPalindrome = (arr) => {
7+
// const reversedArr = arr.reverse();
8+
// console.log('Resversed', reversedArr);
9+
// if (arr === reversedArr) {
10+
// return true;
11+
// }
12+
return arr.every((char, i) => char === arr[arr.length - i - 1]);
1313
}
1414

1515
const minAdjacentSwap = (str) => {
1616
// Edge case: if the length of a string passed is equal to Zero, return undefined;
1717
if (str.length === 0) return -1;
18+
let arr = str.split('');
1819
let count = 0;
1920
let i = 0;
20-
let j = str.length-1;
21+
let j = arr.length-1;
2122
let k;
2223
let done = true;
2324

24-
const isPal = isPalindrome(str);
25+
const isPal = isPalindrome(arr);
2526
console.log('IsPal', isPal);
26-
while(done) {
27-
if (isPalindrome(str)) {
27+
while(i < j) {
28+
if (isPalindrome(arr)) {
2829
done = false;
2930
}
30-
const first = str[i];
31-
const substr = str.indexOf(first, i+1);
32-
// Start swapping from where is find the element.
33-
if (substr > -1) {
34-
if (i == j) {
35-
done = false;
36-
}
37-
// start swap
38-
k = substr;
39-
[str[k], str[k+1]] = [str[k+1], str[k]];
40-
count++;
41-
if (isPalindrome(str)) {
42-
done = false;
43-
}
44-
} else {
45-
// Start swapping from the beginning
46-
[str[i], str[i+1]] = [str[i+1], str[i]];
47-
count++;
31+
// Check if the first and last item are equal,
32+
// then, move i forward and j backward.
33+
console.log(i, j);
34+
console.log(arr[i], arr[j]);
35+
if (arr[i] === arr[j]) {
4836
i++;
49-
console.log('Str', str);
50-
// check if string is palindrome
51-
// const reversedStr = str.split('').reverse().join('');
52-
// console.log('Resversed', reversedStr);
53-
if (isPalindrome(str)) {
54-
done = false;
55-
}
37+
j--;
5638
}
39+
console.log('two', i, j);
40+
console.log('two', arr[i], arr[j]);
41+
const first = arr[i];
42+
const subarr = arr.indexOf(first, i+1);
43+
console.log('arr', arr);
44+
console.log('First', first, subarr);
45+
// Start swapping from where is find the element.
46+
// if (subarr > -1) {
47+
// if (i === j) {
48+
// done = false;
49+
// }
50+
// // start swap
51+
// k = subarr;
52+
// [arr[k], arr[k+1]] = [arr[k+1], arr[k]];
53+
// count++;
54+
// if (isPalindrome(arr)) {
55+
// done = false;
56+
// }
57+
// console.log('after arr', arr);
58+
// } else {
59+
// // Start swapping from the beginning
60+
// [arr[i], arr[i+1]] = [arr[i+1], arr[i]];
61+
// count++;
62+
// i++;
63+
// console.log('arr', arr);
64+
// // check if array is palindrome
65+
// if (isPalindrome(arr)) {
66+
// done = false;
67+
// }
68+
// }
69+
70+
71+
// if (i === j) {
72+
// done = false;
73+
// }
74+
// i++;
75+
i++;
76+
j--;
5777
}
5878

5979
return count;

0 commit comments

Comments
 (0)