Files

127 lines
3.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
id: a7bf700cd123b9a54eef01d5
title: Без повторів, будь ласка
challengeType: 5
forumTopicId: 16037
dashedName: no-repeats-please
---
# --description--
Повертає кількість всіх перестановок заданого рядка, в яких не повторюються літери поспіль. Припустимо, що всі символи в заданому рядку унікальні.
Наприклад, `aab` повинен повернути 2, тому що загалом він має 6 перестановок (`aab`, `aab`, `aba`, `aba`, `baa`, `baa`), але тільки 2 з них ( `aba` та `aba`) не мають повторення однієї і тієї ж букви (в даному випадку `a`).
# --hints--
`permAlone("aab")` має повертати число.
```js
assert.isNumber(permAlone('aab'));
```
`permAlone("aab")` має повертати 2.
```js
assert.strictEqual(permAlone('aab'), 2);
```
`permAlone("aaa")` має повертати 0.
```js
assert.strictEqual(permAlone('aaa'), 0);
```
`permAlone("aabb")` має повертати 8.
```js
assert.strictEqual(permAlone('aabb'), 8);
```
`permAlone("abcdefa")` має повертати 3600.
```js
assert.strictEqual(permAlone('abcdefa'), 3600);
```
`permAlone("abfdefa")` має повертати 2640.
```js
assert.strictEqual(permAlone('abfdefa'), 2640);
```
`permAlone("zzzzzzzz")` має повертати 0.
```js
assert.strictEqual(permAlone('zzzzzzzz'), 0);
```
`permAlone("a")` має повертати 1.
```js
assert.strictEqual(permAlone('a'), 1);
```
`permAlone("aaab")` має повертати 0.
```js
assert.strictEqual(permAlone('aaab'), 0);
```
`permAlone("aaabb")` має повертати 12.
```js
assert.strictEqual(permAlone('aaabb'), 12);
```
# --seed--
## --seed-contents--
```js
function permAlone(str) {
return str;
}
permAlone('aab');
```
# --solutions--
```js
function permAlone(str) {
return permuter(str).filter(function(perm) {
return !perm.match(/(.)\1/g);
}).length;
}
function permuter(str) {
// http://staff.roguecc.edu/JMiller/JavaScript/permute.html
//permArr: Global array which holds the list of permutations
//usedChars: Global utility array which holds a list of "currently-in-use" characters
var permArr = [], usedChars = [];
function permute(input) {
//convert input into a char array (one element for each character)
var i, ch, chars = input.split("");
for (i = 0; i < chars.length; i++) {
//get and remove character at index "i" from char array
ch = chars.splice(i, 1);
//add removed character to the end of used characters
usedChars.push(ch);
//when there are no more characters left in char array to add, add used chars to list of permutations
if (chars.length === 0) permArr[permArr.length] = usedChars.join("");
//send characters (minus the removed one from above) from char array to be permuted
permute(chars.join(""));
//add removed character back into char array in original position
chars.splice(i, 0, ch);
//remove the last character used off the end of used characters array
usedChars.pop();
}
}
permute(str);
return permArr;
}
permAlone('aab');
```