Ad
  • Default User Avatar

    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.

  • Default User Avatar

    This is a really bad solution, at least regarding performance, and definitely not a "Best Practice". These are the issues:

    1. It uses "substring" which is O(n) complexity, plus you have the overhead in memory for creating a new string everytime you use substring. Please see: https://stackoverflow.com/questions/4679746/time-complexity-of-javas-substring
    2. 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:

    1. 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)
    2. 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.

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    Thanks :) I think it counts as a one-liner solution

  • Default User Avatar

    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.

  • Default User Avatar

    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.

  • Default User Avatar

    Thank you very much for the explanation :)

  • Default User Avatar

    I found the solution by observation. But I would like a proper math/logic explanation. Can someone help me please?

  • Default User Avatar

    This is indeed the best practice if you have a "long" parameter. If on the other hand the type parameter was an "int", then it would be safe to use string.repeat(n) as the answer.

  • Default User Avatar

    At first I thought of this solution as well. However, take into account that the parameter is long for some reason. In real life applications this would be a code smell. This is not a safe cast as you have no control over the argument at runtime. At some point, a client may pass a long, as the type parameter is long, and then the casting will fail with an overflow of the long value.

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    This is definitely not a good practice. Whenever you see a literal true or false you should always ask yourself if you can get rid of it by analyzing the boolean expression. Most of the time you only need to return the result of the boolean expression, as in this case.

  • Default User Avatar

    Having a for loop that stops on a condition not related to the defined variable "i" is not a good practice. Usually for loops are used to traverse exhaustively, for example in an array or collection. If you don't know for sure when a loop should stop and you depend on an external condtion as in this case, it is better to use a while loop.

  • Default User Avatar

    You can also use IntSummaryStatistics, which is very nice when trying to get multiple data from an IntStream. I learned this from other Katas. Please see my solution :)

  • Default User Avatar

    While this is a clean solution, it is not a good practice, as it does perform badly: O(n*m), where n and m are the sizes of the arrays. Basically you are going though the second array as many times as items you have in the first array. You can accomplish this going through only once each array. See other solutions :) or mine if you wish.

  • Loading more items...