-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutations.js
58 lines (41 loc) · 1.09 KB
/
permutations.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
48
49
50
51
52
///get all permutations
//use regexp to remove string with repeating charters
function permAlone(str){
var permArr = [], usedChars = [];
//REGULAR EXPRESSION TO MATCH REPEATING CHARACTER IN A STRING
var reg = /(\w)\1+/g;
var param = str;
var answer = permute(param);
function permute(input) {
var i, ch, chars = input.split("");
for (i = 0; i < chars.length; i++) {
ch = chars.splice(i, 1);
usedChars.push(ch);
if (chars.length == 0)
permArr[permArr.length] = usedChars.join("");
permute(chars.join(""));
chars.splice(i, 0, ch);
usedChars.pop();
}
return permArr;
}
//match repeating characters in a string
//parsestring3= "aab".match(reg);
function findRepeatingChar(arr){
var output=[];
for (var i = 0; i < arr.length; i++) {
if(arr[i].match(reg)!=null){
//console.log("A match!");
}
else{
output.push(arr[i]);
}
}
return output;
}
//console.log(answer);
//console.log(findRepeatingChar(answer))
return(findRepeatingChar(answer).length);
}
//cycle through array to create permutations
permAlone("aab");