How is everyone holding up? The last few weeks have been completely unpredictable in the worst ways. So I can only hope that you are all maintainin...
For further actions, you may consider blocking this person and/or reporting abuse
Here is my solution in JavaScript :)
I don't know about this one, somehow @jasterix solution was more friendly to me. I'm not a fan of RegEx because it's really hard to memorize and easy to forget when you are not using it for a week or so (exaggerating)
Luckily (or sometimes badly), everyone finds something different easier to read. I personally like the RegEx approach more, since it directly show what happens to which parts. I am quite used to read RegEx and find it more confusing to think again about what function is doing what and resulting in what.
Very nice! Unfortunately I suck at RegEx :(
Would the Big-O of this be constant?
This one should be O(n). I had the same question in mind and googled it. Never the less, I am actually a big fan of regex :D I have fun writing them.
This is mixing functional, I find it a bit weird, what I'd do:
I'd also move the capitalize first letter to separate function to gain some performance
I was playing around with this a few ways in Python. I wondered if just passing through the characters once would be better than splitting and joining which passes through the words multiple times. First up, make a big string.
First try:
This takes 5.04s (3 s.f.). Maybe looking up the value of the string at
a[i-1]
is too slow, so here's another try using flags to speed things up:The time becomes 4.44s (3 s.f.), so we've shaved a bit off. Then I tried the Python equivalent of @jasterix solution:
The time... 0.0615s (3 s.f.). Now, although the first two pass through each character whereas the last one only passes through the words, surely
split(' ')
needs to examine each character to decide if it is' '
or not? This got me thinking, so I tried:Just going through the string and adding each character to the end of an new string takes 4.20s (3 s.f.) seconds! Compared with
which takes 0.0246s.
In short, I conclude that
split()
andjoin()
are magic and I will use them for building strings over+=
in Python from now on, where possible!I don't think your solution is linear. Split and Join functions have time complexity too. A true O(n) solution would iterate over the string directly(only once) and make changes on the fly. Looping through the given string and capitalizing any character that has a space before can be a O(n) I think.
Well, given split and join are also O(n) then you would have some complexity like
3n + x
which still is O(n) since you would remove all constants.text-transform: capitalize;
I cheeted
So simple