Ad
  • Custom User Avatar

    That's theory on complexity and if we follow, we should say that what you propose is O(n*m) where m is the number of letters a-z i.e. the size of your boolean array. Now, the log(n) part is less than that for even the largest possible memory of a single computer. And while I agree that m can be considered constant, the O(nlogn) solution will be faster in any practical application. That's theory. And when it comes to other things like readability, then, well the code speaks for itself :)

  • Custom User Avatar

    Nevertheless, you come through array for 3 times (2 foreach, 1 for), while solution on the top goes twice. But your idea is pretty interesting.

  • Custom User Avatar

    Being strict, for very long strings this will take O(nlogn); I've used an array of booleans and I set an element to true if its character is found. At the end my solution is O(n). Please take a look at let me know what you think :)