Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
No, it will fail with an
ArrayIndexOutOfBoundsException
.Kata says:
If the passed array has no missed letters, your solution will end with ArrayIndexOutOfBoundsException.
For instance, ['a', 'b', 'c', 'd', 'e'] -> ' '.
errr, possibly, but if you go for readability...?
Please, use spoiler flag next time.
If I had wrote the same code like this I would have been fired from my school... This is a terrible solution, I guess.
To enhance fastness, it would be better to convert the String to StringBuilder, do the replaces like we did with String, and then convert the SB back to String. But it would still remain in O(n), thus it may not be worth the effort, but is nice to know.
Both solutions are in the same complexity class O(n). So it's not really a win to use a for loop, instead of just replacing several times. Your code has also a bad readability, and is therefore less maintainable in production.
Our method will not be slow like you say, only somewhat slower than yours --> See big O notation: https://en.wikipedia.org/wiki/Big_O_notation
Correct, but its more concise than what you did with a boilerplate for loop. One line versus 11 lines on your code. Best way would of been to use regex and call .replace() once.
without seeing your soluiton, it's impossible to answer (don't forget the spoiler flag)
i'd like to solve it in javascript some day. maybe javascript will be even shorter :)
thx!
(You should see the python equivalent of this algorithm, its two times shorter!... if not three... ;) )
(EDIT: actually, it's 4 time shorter... ;-o )
Sorry, I have no idea what you're implying.
The algorythm should work fine for any array with a length > 1 which has char missing from the sequence (which is exactly what the task demands).
The only way for a OOB exception to trigger would be an array containing no missing sequence, thus causing the for loop to actually reach the last element and then test on i+1. But as the kata description assures, the input array will always be valid, thus that's not a concern.