-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithm.txt
33 lines (24 loc) · 1.01 KB
/
Algorithm.txt
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
Given a domain name and dictaionary (constant time word checking)
Return the shortest possible sequence of words included in the domain appearing in order that consists of the longest possible words
given: "12hellobrocrapbruh.net"
output: "12 hello crap bruh"
given "cutecatvideos.com"
output "cute cat videos"
loop over every character (worst case O(n))
/*
helper(index, original, words_arr) {
if (index >= original.length) return words_arr;
current_output = [];
for (i = index; i < original.length; i++) {
substring = original.substr(index, i);
if (substring is a word && word_length > min_length) {
output_words = helper(i, original, {...words_arr, substring});
if (output_words.total_characters > current_output.total_characters) {
current_output = output_words;
} else if (output_words.total_characters == current_output.total_characters) {
if (output_words.length < current_output.length) current_output = output_words;
}
}
}
return current_output;
}