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.
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.
Clever! but can cause performance hit and also not works in reality where something other than Braces involves.
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 inO(n log n)
or searching through an array, which results inO(n^2)
overall. While simple and concise to write, they don't scale as well.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 ofa
didn't have a match (no early return) or ifb
was an empty arraygreat solution :) takes some time to complete though.
@Renaissance8905: I use
guard
any time I can to escape early. In this case, thereturn arr
is an early escape in casetimes
equals zero. So I don't only useguard
in failure cases, but edge cases in general.The added benefit here is that we don't have to scan the
guard
body for areturn
statement, since the compiler enforces it.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.
Nice! I like this solution a lot, but I'd use
guard
rather thanif
:)