Good morning! Here's our prompt for today.
You're given a string of random alphanumerical characters with a length between 0
and 1000
.
Write a method to return the first character in the string that does not repeat itself later on.

So if we had a string input of "asdfsdafdasfjdfsafnnunl'"
, we can see there are multiple letters that are repeated.
Executing firstNonRepeat('asdfsdafdasfjdfsafnnunl')
should return 'j'
since it's the first letter where there is no duplicate.
Constraints
- The given string can be empty
- The string will only contain lowercase/uppercase alphabets and numerals
- Expected time complexity :
O(n)
- Expected space complexity :
O(A)
whereA
is the number of ASCII characters
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
35
​
def first_non_repeat(s):
# fill this in
​
return ""
​
​
import unittest
​
​
class Test(unittest.TestCase):
def test_1(self):
assert first_non_repeat("") == ""
print("PASSED: first_non_repeat('') should return ''")
​
def test_2(self):
assert first_non_repeat("a") == "a"
print("PASSED: first_non_repeat('a') should return 'a'")
​
def test_3(self):
assert first_non_repeat("oh my god dude where is my car") == "g"
print(
"PASSED: first_non_repeat('oh my god dude where is my car') should return 'g'"
)
​
def test_4(self):
assert first_non_repeat("asdfsdafdasfjdfsafnnunlfdffvxcvsfansd") == "j"
print(
"PASSED: first_non_repeat('asdfsdafdasfjdfsafnnunlfdffvxcvsfansd') should return 'j'"
)
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
Here's how we would solve this problem...
How do I use this guide?
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.