--- id: a7bf700cd123b9a54eef01d5 title: No Repeats Please challengeType: 5 forumTopicId: 16037 localeTitle: Нет повторений Пожалуйста --- ## Description
Возвращает число полных перестановок предоставленной строки, которые не имеют повторяющихся последовательных букв. Предположим, что все символы в предоставленной строке уникальны. Например, aab должен возвращать 2, поскольку имеет 6 полных перестановок ( aab , aab , aba , aba , baa , baa ), но только 2 из них ( aba и aba ) не имеют одинаковой буквы (в данном случае a ) повторяющееся. Не забудьте использовать Read-Search-Ask, если вы застряли. Попробуйте подключить программу. Напишите свой собственный код.
## Instructions
## Tests
```yml tests: - text: permAlone("aab") should return a number. testString: assert.isNumber(permAlone('aab')); - text: permAlone("aab") should return 2. testString: assert.strictEqual(permAlone('aab'), 2); - text: permAlone("aaa") should return 0. testString: assert.strictEqual(permAlone('aaa'), 0); - text: permAlone("aabb") should return 8. testString: assert.strictEqual(permAlone('aabb'), 8); - text: permAlone("abcdefa") should return 3600. testString: assert.strictEqual(permAlone('abcdefa'), 3600); - text: permAlone("abfdefa") should return 2640. testString: assert.strictEqual(permAlone('abfdefa'), 2640); - text: permAlone("zzzzzzzz") should return 0. testString: assert.strictEqual(permAlone('zzzzzzzz'), 0); - text: permAlone("a") should return 1. testString: assert.strictEqual(permAlone('a'), 1); - text: permAlone("aaab") should return 0. testString: assert.strictEqual(permAlone('aaab'), 0); - text: permAlone("aaabb") should return 12. testString: assert.strictEqual(permAlone('aaabb'), 12); ```
## Challenge Seed
```js function permAlone(str) { return str; } permAlone('aab'); ```
## Solution
```js function permAlone(str) { return permutor(str).filter(function(perm) { return !perm.match(/(.)\1/g); }).length; } function permutor(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'); ```