2021-05-05 10:13:49 -07:00
|
|
|
|
---
|
|
|
|
|
id: a7bf700cd123b9a54eef01d5
|
2021-10-18 08:17:43 -07:00
|
|
|
|
title: 請不要重複
|
2021-05-05 10:13:49 -07:00
|
|
|
|
challengeType: 5
|
|
|
|
|
forumTopicId: 16037
|
|
|
|
|
dashedName: no-repeats-please
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
返回沒有重複連續字母的提供字符串的總排列數。 假設提供的字符串中的所有字符都是唯一的。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
例如,`aab` 應該返回 2,因爲它有 6 個排列(`aab`、`aab`、`aba`、`aba`、`baa` 和 `baa`),但其中只有 2 個(`aba` 和 `aba`)沒有重複相同字符(即 `a`)。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
`permAlone("aab")` 應返回 2。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.isNumber(permAlone('aab'));
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
`permAlone("aab")` 應該返回 2。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(permAlone('aab'), 2);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
`permAlone("aaa")` 應該返回 0。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(permAlone('aaa'), 0);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
`permAlone("aabb")`應返回 8。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(permAlone('aabb'), 8);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
`permAlone("abcdefa")`應返回 3600。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(permAlone('abcdefa'), 3600);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
`permAlone("abfdefa")`應該返回 2640。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(permAlone('abfdefa'), 2640);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
`permAlone("zzzzzzzz")` 應返回 0。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(permAlone('zzzzzzzz'), 0);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
`permAlone("a")` 應該返回 1。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(permAlone('a'), 1);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
`permAlone("aaab")` 應該返回 0。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert.strictEqual(permAlone('aaab'), 0);
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-18 08:17:43 -07:00
|
|
|
|
`permAlone("aaabb")` 應返回 12.
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
```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');
|
|
|
|
|
```
|