-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutation_first_solution.js
61 lines (48 loc) · 1.07 KB
/
permutation_first_solution.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// inputs
t="ABDCDFES"
s="ADBC"
// sampels
"ADBC"
"ABDC"
const s_char_list = s.split("");
var permArr = [],
usedChars = [];
function permute(input) {
var i, ch;
for (i = 0; i < input.length; i++) {
ch = input.splice(i, 1)[0];
usedChars.push(ch);
if (input.length == 0) {
permArr.push(usedChars.slice());
}
permute(input);
input.splice(i, 0, ch);
usedChars.pop();
}
return permArr;
};
function generate_word_list(char_list){
const word_list_array = permute(char_list);
const word_list = word_list_array.map(
(current_word_array)=>
{
return current_word_array.join('');
}
)
console.log(word_list);
return word_list;
}
const found_word = generate_word_list(s_char_list).filter(
(current_word) =>
{
const isExist = t.includes(current_word);
if
(
isExist
)
{
return current_word;
}
}
)
console.log(found_word);