We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Agree with you on all comments except for toCharArray() and the failing part.
toCharArray() is wasteful here since it creates a new array (a copy) and allocates memory to store all the characters from the string. For small strings, this might not be an issue, but for longer ones you're wasting both time and space. Look at this post regardin the performance of using charAt(i) vs toCharArray() for character iteration.
The failing part is handled already in my routine (look at the !failed part in my for loop condiiton). I'd usually prefer a single return statement in a method rather than having multiple points exiting the routine.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Stack
You are viewing a single comment's thread. Return to all comments →
Agree with you on all comments except for
toCharArray()
and the failing part.toCharArray()
is wasteful here since it creates a new array (a copy) and allocates memory to store all the characters from the string. For small strings, this might not be an issue, but for longer ones you're wasting both time and space. Look at this post regardin the performance of usingcharAt(i)
vstoCharArray()
for character iteration.The failing part is handled already in my routine (look at the
!failed
part in my for loop condiiton). I'd usually prefer a single return statement in a method rather than having multiple points exiting the routine.