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.
Passionate about building great technology and connecting with people to create positive change. Happy to answer questions about transitioning to tech. Find me on Twitter @lounecl
This is mixing functional, I find it a bit weird, what I'd do:
functioncapitalize(str){// Split string into an array of wordsconststring=str.split("");// Convert every first letter to uppercasereturnstring.map((word)=>{returnword[0].toUpperCase()+word.slice(1).toLowerCase();}).join("");}
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.
fromtimeimporttimea='hello this is my string it is lovely!'*10000a=a.strip()
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:
A=''t=time()forcina:A+=cprint(time()-t)
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
t=time()A=''.join(cforcina)print(time()-t)
which takes 0.0246s.
In short, I conclude that split() and join() are magic and I will use them for building strings over += in Python from now on, where possible!
How’s it going, I'm a Adam, a Full-Stack Engineer, actively searching for work. I'm all about JavaScript. And Frontend but don't let that fool you - I've also got some serious Backend skills.
Location
City of Bath, UK 🇬🇧
Education
11 plus years* active enterprise development experience and a Fine art degree 🎨
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.
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!text-transform: capitalize;
I cheeted
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 + xwhich still is O(n) since you would remove all constants.So simple