Ad
  • Default User Avatar

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

  • Default User Avatar

    Wrong: len is O(1), since the length is stored as a property in any object.

  • Custom User Avatar

    @garborg, return len(string) > 26 will be O(n) for only some of the test cases, but not all.
    At best, it is an incomplete solution, right?

  • Default User Avatar

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

  • Default User Avatar

    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.

  • Default User Avatar

    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.

  • Default User Avatar

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

  • Default User Avatar

    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.

  • Default User Avatar

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

  • Default User Avatar

    Thanks garborg. But what is the meaning of the O(n^2)

  • Custom User Avatar

    True about O(n^2). What is the O(1) version?

  • Default User Avatar

    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.

  • Default User Avatar

    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.