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.
This is not true with every object. More - an alphabet may contain more or less symbols than latin alphabet. There is no hints in the task that it is latin alphabet. That is why solution O(1) does not exist. Best complexity is O(n) where n is the size of the alphabet. I belive that you need О(n) memory for that complexity. This solution complexity is О(n^2).
Wrong:
len
is O(1), since the length is stored as a property in any object.@garborg,
return len(string) > 26
will beO(n)
for only some of the test cases, but not all.At best, it is an incomplete solution, right?
Any string that is longer than the length of the alphabet WILL necessarily contain duplicates (pigeonhole principle), and is therefore not an isogram. The running time for an algorithm that takes that into account is bounded above by the maximum running time on a sentence < 27 characters + the time to ascertain if a string is 27 characters or longer. That is O(1).
I believe that no one can do it with O(1). The best possible solution O(n) where n is all possible symbols in an alphabet.
It is both, so it is in fact θ(1).
The time taken to process any string is bounded above by the time to process the most difficult string on 26 characters.
This comment is hidden because it contains spoiler information about the solution
O-notation is used to indicate how the worst-case running time scales with the input. Basically, in the worst case scenario, a string that is twice as long takes ~4 times as long to process.
Hehe, so, I was actually making two separate points, and I should have made them separately.
(1) The code is inefficient because it is overcounting, and scanning through the string can be done in linear time.
(2) With some cleverness, there is a modification that turns any algorithm for this problem into an O(1) algorithm. Just observe that strings of certain lengths cannot be isograms. (Hint: pigeonhole principle.)
Thanks garborg. But what is the meaning of the O(n^2)
True about O(n^2). What is the O(1) version?
Nice maintainable code, but maybe not the best solution in terms of algorithmics. String.count has linear running time in the length of the string, and the loop is running along the entire string. The worst-case running time of this solution is therefore O(n^2), when an O(1) solution is achievable.
This is pretty elegant in terms of the length of the code, but it is not very inefficient. It will recount elements, so that an element that occurs 2N times for a large number N, will lead to a recount that many times. Counting elements is linear in the number of elements, so the time complexity is O(N^2). This problem can be solved easily in O(n) time.