You must match T titles against a dictionary of W words, and your current code runs in O(T ×ばつ W) time.
For at least some of your thousands of strings
(and for all eleven that appeared in your example)
the spec is to find each title that .startsWith
this single-word regex: ^(\w+):
So create a Set()
lexicon containing all those words.
Then for each title, use that regex to parse out the
leading word (if any), and look it up in the the lexicon.
If found, perform the .replace operation.
That's O(1) constant time per title,
total of O(T) time for the page.
If you need a leading phrase rather than leading word,
use ^([ \w]+):
or similar expression.
You must match T titles against a dictionary of W words, and your current code runs in O(T ×ばつ W) time.
For at least some of your thousands of strings
(and for all that appeared in your example)
the spec is to find each title that .startsWith
this single-word regex: ^(\w+):
So create a Set()
lexicon containing all those words.
Then for each title, use that regex to parse out the
leading word (if any), and look it up in the the lexicon.
If found, perform the .replace operation.
That's O(1) constant time per title,
total of O(T) time for the page.
If you need a leading phrase rather than leading word,
use ^([ \w]+):
or similar expression.
You must match T titles against a dictionary of W words, and your current code runs in O(T ×ばつ W) time.
For at least some of your thousands of strings
(and for all eleven that appeared in your example)
the spec is to find each title that .startsWith
this single-word regex: ^(\w+):
So create a Set()
lexicon containing all those words.
Then for each title, use that regex to parse out the
leading word (if any), and look it up in the the lexicon.
If found, perform the .replace operation.
That's O(1) constant time per title,
total of O(T) time for the page.
If you need a leading phrase rather than leading word,
use ^([ \w]+):
or similar expression.
You must match T titles against a dictionary of W words, and your current code runs in O(T ×ばつ W) time.
For at least some of your thousands of strings
(and for all that appeared in your example)
the spec is to find each title that .startsWith
this single-word regex: ^(\w+):
So create a Set()
lexicon containing all those words.
Then for each title, use that regex to parse out the
leading word (if any), and look it up in the the lexicon.
If found, perform the .replace operation.
That's O(1) constant time per title,
total of O(T) time for the page.
If you need a leading phrase rather than leading word,
use ^([ \w]+):
or similar expression.