main.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
function permAlone(str) {
  var n = str.length;
  var arai = str.split('');
  var repeatRegex = /(.)\1{1,}/g;
  
  function swap(a, b){
    var temp = arai[a];
    arai[a] = arai[b];
    arai[b] = temp;
  }
  
  function heapAlgo(int, arr){
    var c = [];
    var result = [];
    
    for(var i = 0; i < int; i++){
      c[i] = 0;
    }
    result.push(arr.join(''));
    
    var j = 0;
    while (j < int){
      if(c[j] < j){
        if(j%2 === 0){
          swap(0, j);
        } else {
          swap(c[j], j);
        }
        result.push(arr.join(''));
        c[j] += 1;
        j = 0;
      } else {
        c[j] = 0;
        j += 1;
      }
    }
    return result;
  }
  
  return heapAlgo(n, arai)
  .reduce(function(count, item){
    item.match(repeatRegex) === null ? count ++ : count += 0;
    return count;
  },0);
}

permAlone('aab');
Native Browser JavaScript