-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudyy.js
More file actions
57 lines (52 loc) · 1.25 KB
/
studyy.js
File metadata and controls
57 lines (52 loc) · 1.25 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const word = "UUUUU";
const result = 3905;
// const word = "I";
// const result = 1563;
// const word = "EIO";
// const result = 1189;
// const word = "AAAE";
// const result = 10;
//*BFS
function solution(word) {
const alphabets = ["A", "E", "I", "O", "U"];
const queue = [];
let answer = 0;
for (let i = 0; i < 5; i++) {
queue.push(alphabets[i]);
}
while (queue.length > 0) {
const curWord = queue.shift(); // A = 1 // AA = 2 queue = [AE,AI,AO,AU,E,I,O,U] .....
// console.log(curWord, queue);
answer++;
if (curWord === word) {
break;
}
for (let i = 0; i < 5; i++) {
const newWord = curWord + alphabets[i];
if (newWord.length <= 5) {
queue.push(newWord);
}
}
queue.sort();
}
return answer;
}
//* DFS
// function solution(word) {
// const words = [];
// const alphabets = ["A", "E", "I", "O", "U"];
// function dfs(curWord, len) {
// if (len > 5) return;
// words.push(curWord);
// for (let i = 0; i < 5; i++) {
// dfs(curWord + alphabets[i], len + 1);
// }
// }
// dfs("", 0);
// words.sort();
// // console.log(words);
// return words.indexOf(word);
// }
console.time("solution");
console.log(solution(word));
console.timeEnd("solution");