Ad
  • Custom User Avatar

    While it's a correct approach, it's not the most efficient due to the repeated string manipulation and the use of deprecated characters property.

  • Custom User Avatar

    Clever! but can cause performance hit and also not works in reality where something other than Braces involves.

  • Custom User Avatar

    I agree with antong01. This problem is best solved in O(n) by not sorting either array, but instead making use of a dictionary. Most of the solutions submitted either use sorting which results in O(n log n) or searching through an array, which results in O(n^2) overall. While simple and concise to write, they don't scale as well.

  • Default User Avatar

    While this is a simple solution, it has complexity of O(n log n); For big arrays it could be suboptimal if the first first element of a didn't have a match (no early return) or if b was an empty array

  • Custom User Avatar

    great solution :) takes some time to complete though.

  • Custom User Avatar

    @Renaissance8905: I use guard any time I can to escape early. In this case, the return arr is an early escape in case times equals zero. So I don't only use guard in failure cases, but edge cases in general.

    The added benefit here is that we don't have to scan the guard body for a return statement, since the compiler enforces it.

  • Custom User Avatar

    Thanks! I'm curious, what's your criteria for using guard vs. if? I typically only use guard when failing the guard would be interpreted as a failure case for the method, or when I'm trying to unwrap an optional that I'd need to use later in the method.

  • Custom User Avatar

    Nice! I like this solution a lot, but I'd use guard rather than if :)