Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I think you can use a queue of size w.

Something like this... this is psuedo code. You iterate through the string once. You pop an element off of the queue at most once. In this implementation you might add it to the queue twice.

def find_uniq_window(long_string, w):

    queue = Queue()
    hash_set = Hashset()

    for char in long_string:
        if queue.size() == w:
           old_char = queue.pop()
           hash_set.remove(old_char)
        queue.push(char)
    
        if char not in hash_set and queue.size() == w:
            # success
            return queue
        if char in hash_set:
            #empty the queue and hash set and start over
            queue = Queue()
            hash_set = HashSet()   
            queue.push(char)
            hash_set.add(char)
  
    # We never found the window of unique characters, so return false
    return False


Instead of a queue, you could just use two indices/pointers to the start and end of the current window.

Also I think on `char in hash_set` you'd want to advance the start of the window until uniqueness is restored, rather than moving start to the end.

E.g. if the string is "abac", when you get to the second "a", instead of restarting the window at the second "a" you want to instead move the start to "b". You might even be able to do something boyer-moore-esque if the hashset is instead a hashmap of chars to index. Then you can directly advance your window by how far in the mismatch is, which probably won't affect asymptotic complexity since you still need to remove elements but allows you to do it in bulk rather than one at a time.


Yes, you are right about advancing the start of the window until uniqueness is restored. So, basically just advance it by 1.

You could definitely use two indexes instead of a queue, and using indexes over an array will be faster than using a queue.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: