-
Notifications
You must be signed in to change notification settings - Fork 1
/
no_repeats.js
47 lines (40 loc) · 962 Bytes
/
no_repeats.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function getPerms(str){
var permutations = [],
nextPerm = [],
chars = str.split('');
function permutate(chars){
if( chars.length === 0 ) {
permutations.push(nextPerm.join(''));
}
for( var i = 0; i < chars.length; i++ ){
chars.push(chars.shift());
nextPerm.push(chars[0]);
permutate(chars.slice(1));
nextPerm.pop();
}
}
permutate(chars);
console.log(permutations);
return permutations;
}
function checkForRepeats(word) {
for( var i = 0; i < word.length - 1; i++) {
if ( word[i] === word[i+1]) {
return true;
}
}
return false;
}
function permAlone(str) {
var allPerms = getPerms(str);
console.log(allPerms);
var noRepeatsPerms = [];
for( var i = 0; i < allPerms.length; i++ ) {
if ( checkForRepeats(allPerms[i]) === false ) {
noRepeatsPerms.push(allPerms[i]);
}
}
console.log(noRepeatsPerms);
return noRepeatsPerms.length;
}
permAlone('aab');