Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your solution definitely fits the criteria of O(n) time. Instead of an array, which would be very, very large if the allowed alphabet is large (Unicode has over a million characters), you could use a plain hash. Here is your algorithm in (unoptimized!) Ruby:</p> <pre><code>def undup(s) seen = Hash.new(0) s.each_char {|c| seen[c] += 1} result = "" s.each_char {|c| result &lt;&lt; c if seen[c] == 1} result end puts(undup "") puts(undup "abc") puts(undup "Olé") puts(undup "asdasjhdfasjhdfasbfdasdfaghsfdahgsdfahgsdfhgt") </code></pre> <p>It makes two passes through the string, and since hash lookup is less than linear, you're good.</p> <p>You can say the Hashtable (like your array) uses constant space, albeit large, because it is bounded above by the size of the alphabet. Even if the size of the alphabet is larger than that of the string, it still counts as constant space.</p> <p>There are many variations to this problem, many of which are fun. To do it truly in place, you can sort first; this gives O(n log n). There are variations on merge sort where you ignore dups during the merge. In fact, this "no external hashtable" restriction appears in <a href="https://stackoverflow.com/questions/1532819/algorithm-efficient-way-to-remove-duplicate-integers-from-an-array">Algorithm: efficient way to remove duplicate integers from an array</a> (also tagged interview question).</p> <p>Another common interview question starts with a simple string, then they say, okay now a million character string, okay now a string with 100 billion characters, and so on. Things get very interesting when you start considering Big Data.</p> <p>Anyway, your idea is pretty good. It can generally be tweaked as follows: Use a set, not a dictionary. Go trough the string. For each character, if it is not in the set, add it. If it is, <em>delete</em> it. Sets take up less space, don't need counters, and can be implemented as bitsets if the alphabet is small, and this algorithm does not need two passes.</p> <p>Python implementation: <a href="http://code.activestate.com/recipes/52560-remove-duplicates-from-a-sequence/" rel="nofollow noreferrer">http://code.activestate.com/recipes/52560-remove-duplicates-from-a-sequence/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload