admin管理员组文章数量:1314514
I am looking for this stackoverflow question to be answered in Javascript.
So if my input is "word", the function should return:
word, Word, wOrd, WOrd, woRd, WoRd, etc..
here's what i have so far but it only produces the permutations (doesn't capitalize anything)
var perm = function(str){
var results = [];
var bos = function(reference, appendTo){
appendTo = appendTo || "";
if(reference.length === 0) {
results.push(appendTo);
}
for(var i = 0; i < reference.length; i++){
var current = reference.splice(i, 1);
bos(reference, appendTo+current);
reference.splice(i, 0, current)
}
}
bos(str.split(""));
return results;
}
perm("word");
I am looking for this stackoverflow question to be answered in Javascript.
So if my input is "word", the function should return:
word, Word, wOrd, WOrd, woRd, WoRd, etc..
here's what i have so far but it only produces the permutations (doesn't capitalize anything)
var perm = function(str){
var results = [];
var bos = function(reference, appendTo){
appendTo = appendTo || "";
if(reference.length === 0) {
results.push(appendTo);
}
for(var i = 0; i < reference.length; i++){
var current = reference.splice(i, 1);
bos(reference, appendTo+current);
reference.splice(i, 0, current)
}
}
bos(str.split(""));
return results;
}
perm("word");
Share
Improve this question
edited May 23, 2017 at 10:30
CommunityBot
11 silver badge
asked Jan 16, 2015 at 20:22
user3068590user3068590
591 silver badge5 bronze badges
6
- So where's your code? I can't see anything to ment on. – Mörre Commented Jan 16, 2015 at 20:23
- I'm going to go ahead and close this as a "work order." If this is a troubleshooting question, show us the work you have so far, and describe the difficulty you are having solving the problem. – Robert Harvey Commented Jan 16, 2015 at 20:24
- just like, you know, make a million randomy upper and lower case versions, then filter duplicates – dandavis Commented Jan 16, 2015 at 20:32
- 1 @dandavis: The actual solution is a bit more... focused than that. – Robert Harvey Commented Jan 16, 2015 at 20:32
- @RobertHarvey: my answer is as focused as this question is on the ways of SO... – dandavis Commented Jan 16, 2015 at 20:34
3 Answers
Reset to default 7One option would be generating capitalization permutations via binary logic.
As a simple example of the snippet below, consider the following table where the left column is the binary representation of the current permutation and the right column is the resulting capitalization:
0000 | word
1000 | Word
0100 | wOrd
1100 | WOrd
...
1111 | WORD
// Used to display the results
const write = (msg) => {
document.body.appendChild(document.createElement('div')).innerHTML = msg;
};
const input = "word";
const letters = input.split("");
const permCount = 1 << input.length;
for (let perm = 0; perm < permCount; perm++) {
// Update the capitalization depending on the current permutation
letters.reduce((perm, letter, i) => {
letters[i] = (perm & 1) ? letter.toUpperCase() : letter.toLowerCase();
return perm >> 1;
}, perm);
const result = letters.join("");
write(result);
}
Note that theoretically this approach would work all the way up to Number.MAX_SAFE_INTEGER
, up to inputs of length 52, but realistically you'll run into performance issues.
var s = "word";
var sp = s.split("");
for (var i = 0, l = 1 << s.length; i < l; i++) {
for (var j = i, k = 0; j; j >>= 1, k++) {
sp[k] = (j & 1) ? sp[k].toUpperCase() : sp[k].toLowerCase();
}
var st = sp.join("");
var d = document.createElement("p");
d.appendChild(document.createTextNode(st));
document.body.appendChild(d);
}
With Nit's solution in mind, I wanted to offer a slightly refactored solution that may be easier to follow
var perm = function(str){
var results = [];
var arr = str.split("");
var len = Math.pow(arr.length, 2);
for( var i = 0; i < len; i++ ){
for( var k= 0, j = i; k < arr.length; k++, j >>=1){
arr[k] = ( j & 1 ) ? arr[k].toUpperCase() : arr[k].toLowerCase();
}
var bo = arr.join("");
results.push(bo);
}
return results;
}
perm("word");
本文标签: permutationFind all lowercase and uppercase combinations of a string in JavascriptStack Overflow
版权声明:本文标题:permutation - Find all lowercase and uppercase combinations of a string in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741934637a2405779.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论