Today's objective is to create a function that takes in a string and returns the first non-repeated character. For instance, in the string 'abcdab', your function should return 'c'. Bonus points for a solution that solves this in linear run time.
Today's objective is to create a function that takes in a string and returns the first non-repeated character. For instance, in the string 'abcdab', your function should return 'c'. Bonus points for a solution that solves this in linear run time.
Permalink: http://problemotd.com/problem/no-repeat/
Content curated by @MaxBurstein
Comments:
Foppe - 11 years ago
reply permalink
Chuisi - 11 years ago
Similar but using dictionaries to provide linear insertion and search times to drop the overall time from O(n2 ) to O(n)
reply permalink
Driphter - 11 years ago
Clojure!
reply permalink
Driphter - 11 years ago
c#
c# (Linq)
reply permalink
rossthebossperot - 11 years ago
Python. Pretty sure this would be linear. I'm not 100% clear how Python's string.count function works.
reply permalink
Max Burstein - 11 years ago
I don't think this would find the first repeated character though I'm not sure about string.count either. You could run in to a case like 'abaab' where a is still the right answer.
reply permalink
Walker Crouse - 11 years ago
reply permalink
asheehan - 11 years ago
in Ruby
reply permalink