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 the difference is negligible in this case, constant time or O(1) is not inherently faster than linear time O(n) where n is relatively small (n <= 40). Because we are counting letters and single digits, that gives us 36 possible values that must be counted (26 letters + 10 digits).
In this case, it would actually make more sense to store the count of each value in an array of pre-defined length.
I'm aware this is a trivial distinction in this case, but I think there's value in educating others that O(1) is not ALWAYS better than O(n).
I don't think there's much of a difference, at least performance wise, to calling lower() on the whole string or each individual character. Strings are, in actuality, just character arrays. By calling lower() on the whole string, the function must still be applied to each individual character.
Similar code, nice
Also, looking up if an element is in a set happens in a constant time, i. e., it always takes the same amount of time, regardless of the size of the set. Looking up if an element is in a list happens in a linear time, i. e. in the worst case, it has to go over all the elements of the list, and here, it would be very inefficient since we have to search the data structures repeatedly.
Hey, can anyone tell me how this can be useful in real life example? I can understand what happened here, but it looks more complicated than the other solutions. Is it a method that has more practicle use in real life projects?
Hope I wasn't being rude.
This looks more efficient than the top rated one on the list, but there can be some improvements. By adding
text = text.lower()
you won't repeatedly call the lower() function, which helps with efficiency. Also, you can use a dictionary rather than two sets. I'm not a pro at python or even close to it, but I'm making an effor to review solutions to see how I could change it to make improvements. I even came to the dictionary conclusion by asking chatgpt (facepalm), so I refuse to take any credit! Nice solution regardless :).Nice and clean solution.
Smart optimisation!
great!
cause the element in set() is single, and it will delete the repeated
This comment is hidden because it contains spoiler information about the solution
My solution was almost identical. If you place "seen.add(char)" inside an else then speed will be slighly improved.
If you have a one million characters text then after a few iterations 99.99% of the characters will be already in 'seen' set so that line of code could be skipped most of the time.
Can't be changed
And increases efficiency
This comment is hidden because it contains spoiler information about the solution
Loading more items...