-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_permutations_by_changing_case.js
More file actions
37 lines (34 loc) · 1.35 KB
/
Copy pathstring_permutations_by_changing_case.js
File metadata and controls
37 lines (34 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const find_letter_case_string_permutations = function(str) {
let permutations = [str];
//scan and create an array of indexes where case needs to change
let indexes = [];
for (let i=0; i<str.length;i++) {
let char = str[i];
if ((char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z')) {
indexes.push(i);
}
}
//iterate over the index and push to base array
indexes.forEach(element => {
let temp = [...permutations];
//for each element of permutations, perform the flip
temp.forEach(x => {
//NOTE: because to replace character in a string, need to convert it to an array first
let charArray = x.split('');
let char = charArray[element];
if (char >= 'a' && char <= 'z')
{
charArray[element] = char.toUpperCase();
permutations.push(charArray.join(''));
}
if (char >= 'A' && char <= 'Z')
{
charArray[element] = char.toLowerCase();
permutations.push(charArray.join());
}
});
});
return permutations;
};
console.log(`String permutations are: ${find_letter_case_string_permutations("ad52")}`)
console.log(`String permutations are: ${find_letter_case_string_permutations("ab7c")}`)