Skip to content

Commit 8214038

Browse files
committed
Still working on min adjacent swap.
1 parent 82d3cdb commit 8214038

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

palindrome/min-adjacent-swap.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* @author: Chisimdi Damian Ezeanieto
3+
* @date: 29/01/2020
4+
*/
5+
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;
13+
}
14+
15+
const minAdjacentSwap = (str) => {
16+
// Edge case: if the length of a string passed is equal to Zero, return undefined;
17+
if (str.length === 0) return -1;
18+
let count = 0;
19+
let i = 0;
20+
let j = str.length-1;
21+
let k;
22+
let done = true;
23+
24+
const isPal = isPalindrome(str);
25+
console.log('IsPal', isPal);
26+
while(done) {
27+
if (isPalindrome(str)) {
28+
done = false;
29+
}
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++;
48+
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+
}
56+
}
57+
}
58+
59+
return count;
60+
};
61+
62+
module.exports = minAdjacentSwap;

palindrome/test.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,35 @@
33
* @date: 12/02/2019
44
*/
55
const palindrome = require('./index');
6+
const minAdjacentSwap = require('./min-adjacent-swap');
67
test('Palindrome is a function', () => {
7-
expect(typeof palindrome).toEqual('function');
8+
expect(typeof palindrome).toEqual('function');
89
});
910
test('Palindrome Hello to give false', () => {
10-
expect(palindrome('Hello')).toEqual(false);
11+
expect(palindrome('Hello')).toEqual(false);
1112
});
1213
test('Palindrome abba to give true', () => {
13-
expect(palindrome('abba')).toEqual(true);
14+
expect(palindrome('abba')).toEqual(true);
1415
});
1516
test('Palindrome ababa to give true', () => {
16-
expect(palindrome('ababa')).toEqual(true);
17+
expect(palindrome('ababa')).toEqual(true);
1718
});
1819
test(`Palindrome '' to give undefined`, () => {
19-
expect(palindrome('')).toEqual(undefined);
20+
expect(palindrome('')).toEqual(undefined);
21+
});
22+
23+
test('minAdjacentSwap is a function', () => {
24+
expect(typeof minAdjacentSwap).toEqual('function');
25+
});
26+
test('minAdjacentSwap mamad to give 3', () => {
27+
expect(minAdjacentSwap('mamad')).toEqual(3);
28+
});
29+
test('minAdjacentSwap asflkj to give -1', () => {
30+
expect(minAdjacentSwap('asflkj')).toEqual(-1);
31+
});
32+
test('minAdjacentSwap aabb to give 2', () => {
33+
expect(minAdjacentSwap('aabb')).toEqual(2);
34+
});
35+
test(`minAdjacentSwap ntiin to give 1`, () => {
36+
expect(minAdjacentSwap('ntiin')).toEqual(1);
2037
});

0 commit comments

Comments
 (0)