I don't see any 'good' or 'bad' practices here.
Please, correct me if I'm wrong.
For me, this question is rather a matter of taste. Because at the end of the day you end with the same instructions, the only difference is the structure of the code.
Hence there are no other differences the choice must be based on readability. And as for me, for-loop is more readable due to its conciseness.
Not sure about this solution. While the code looks simpler it is not a best practice. It creates a new object everytime you have a consecutive word. In this case it isn't a big deal, but in general it is better to avoid the creation of objects like this, as they are just temporal, and their lifetime is within a loop. In more complex scenarios this could lead to waste of memory and bad performance. It is not a good practice.
It uses the contains mehtod of a list to check for every iteration! This is really bad, each iteration is O(n), so you will have 1 element, then 2, then 3, etc. That is equal to 1 + 2 + 3 = n*(n+1)/2, which is quadratic.
To fix them:
You can work with the char array representation of the string, and swap the elements, that way you never create unnecesary intermediate strings, just the final one, and is constant time O(1)
You can use a set, which has constant time lookup O(1)
Please see my answer or others that have use the same approach. But please be careful whenever you use some methods, always think about the complexity and the memory usage of your implementations.
Also computationally inefficient, because each time an element gets evaluated, it is compared with all the existing lists. But I loved the functional approach.
Interesting solution, yet it is very expensive in terms of space. You are creating a new list for every range, even if it is a one value range. So in worst case you would have n lists where n is the size of the array.
Smart! I didn't think that taking the modulus would work like that.
I don't see any 'good' or 'bad' practices here.
Please, correct me if I'm wrong.
For me, this question is rather a matter of taste. Because at the end of the day you end with the same instructions, the only difference is the structure of the code.
Hence there are no other differences the choice must be based on readability. And as for me, for-loop is more readable due to its conciseness.
still no answer, some kind of magic... :)
This comment is hidden because it contains spoiler information about the solution
I was worried the Garbage Collector might get bored if it had nothing to do.
Not sure about this solution. While the code looks simpler it is not a best practice. It creates a new object everytime you have a consecutive word. In this case it isn't a big deal, but in general it is better to avoid the creation of objects like this, as they are just temporal, and their lifetime is within a loop. In more complex scenarios this could lead to waste of memory and bad performance. It is not a good practice.
This is a really bad solution, at least regarding performance, and definitely not a "Best Practice". These are the issues:
To fix them:
Please see my answer or others that have use the same approach. But please be careful whenever you use some methods, always think about the complexity and the memory usage of your implementations.
This comment is hidden because it contains spoiler information about the solution
Also computationally inefficient, because each time an element gets evaluated, it is compared with all the existing lists. But I loved the functional approach.
Thanks :) I think it counts as a one-liner solution
Interesting answer
Interesting solution, yet it is very expensive in terms of space. You are creating a new list for every range, even if it is a one value range. So in worst case you would have n lists where n is the size of the array.
You're right, although String.repeat() was only added since Java 11 and won't work on java 8...
The bound + 1 / 2 is a nice trick that is useful in many situations where you need the same index for odd and even lengths.
Thank you very much for the explanation :)
Loading more items...