* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
3.0 KiB
3.0 KiB
id, title, challengeType, videoUrl, dashedName
id | title | challengeType | videoUrl | dashedName |
---|---|---|---|---|
a7bf700cd123b9a54eef01d5 | 请不要重复 | 5 | no-repeats-please |
--description--
返回没有重复连续字母的提供字符串的总排列数。假设提供的字符串中的所有字符都是唯一的。例如, aab
应该返回2,因为它总共有6个排列( aab
, aab
, aba
, aba
, baa
, baa
),但只有2个( aba
和aba
)没有相同的字母(在这种情况下为a
)重复。如果卡住,请记得使用Read-Search-Ask 。尝试配对程序。编写自己的代码。
--hints--
permAlone("aab")
应返回一个数字。
assert.isNumber(permAlone('aab'));
permAlone("aab")
应返回2。
assert.strictEqual(permAlone('aab'), 2);
permAlone("aaa")
应该返回0。
assert.strictEqual(permAlone('aaa'), 0);
permAlone("aabb")
应该返回8。
assert.strictEqual(permAlone('aabb'), 8);
permAlone("abcdefa")
应返回3600。
assert.strictEqual(permAlone('abcdefa'), 3600);
permAlone("abfdefa")
应返回2640。
assert.strictEqual(permAlone('abfdefa'), 2640);
permAlone("zzzzzzzz")
应该返回0。
assert.strictEqual(permAlone('zzzzzzzz'), 0);
permAlone("a")
应返回1。
assert.strictEqual(permAlone('a'), 1);
permAlone("aaab")
应该返回0。
assert.strictEqual(permAlone('aaab'), 0);
permAlone("aaabb")
应该返回12。
assert.strictEqual(permAlone('aaabb'), 12);
--seed--
--seed-contents--
function permAlone(str) {
return str;
}
permAlone('aab');
--solutions--
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');