Ad
  • 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

    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

    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.