Python Challenge - Unique Characters in a String

This community-built FAQ covers the “Unique Characters in a String” code challenge in Python. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the Python challenge Unique Characters in a String

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

def unique_characters(string_in):
  letter = {}
  if string_in == "":
    print("ERROR")
    return 
  for l in string_in:
    if(letter.get(l)):
      return False
    else:
      letter[l] = True
  return True

print(unique_characters("apple"))

def unique_characters(string_in):
unique_list =
flag = True
if string_in =="":
return ‘error, empty string’
else:
for i in string_in:
if i not in unique_list:
unique_list.append(i)
else:
flag= False
return flag

print(unique_characters(“apple”))

def unique_characters(string_in): if string_in == "": print("Error") else: return len(string_in) == len({x for x in string_in}) print(unique_characters("apple"))

image
check replies

Using your code, unique_characters("afirm") should return True, but returns False.

thanks the tests said it was good so I didn’t even check. I’ll upload a correct one

1 Like

1 Like

I noticed the tests are obviously inadequate for this challenge.

def unique_characters(string_in): letters_list = [] for letter in string_in: if letter not in letters_list: letters_list.append(letter) else: return False if string_in in "".join(letters_list): return True
def unique_characters(string_in): if string_in == "": return "error" return sorted(set(string_in)) == sorted(string_in) print(unique_characters("iocqnrux9182m"))

btw your code can be simplified like above. If you pass a string to set() it will convert it, like passing to list()

our solution is really similar :laughing:

Here’s mine!

def unique_characters(string_in): if string_in == '': return 'Error: Empty string.' letter_list = [] for char in string_in: if char not in letter_list: letter_list.append(char) if len(letter_list) < len(string_in): return False else: return True print(unique_characters("apple"))
1 Like
def unique_characters(string_in): if not string_in: return 'Invalid string' return len(set(string_in)) == len(string_in)
def unique_characters(string_in):
  f = filter(lambda c: c  if string_in.count(c) > 1 else [], string_in)
  if string_in == "":
    return ValueError()
  else: return len(list(f)) < 1


print(unique_characters("apple"))
def unique_characters(string_in):
  if string_in=='':
    return "Error, empty input"
  else:
    return len(string_in)==len(set(string_in))
   
print(unique_characters("apple"))

def unique_characters(string_in):
string_in = string_in.lower()
y = 0
for i in range(0, len(string_in)):
for i in string_in:
x = string_in.count(i)
if x >= 2:
y +=1
if y != 0:
return False
else:
return True

print(unique_characters(“apple”))

What do you guys think about mine, I feel like it can be shortened.

def unique_characters(string_in):
  if string_in and len(string_in) > 0:
    for x in string_in:
      if string_in.count(x) > 1:
        return False
    return True

print(unique_characters("apple"))
def unique_characters(string_in):
  if string_in == '':
    return 'error'
  new_string = ''
  for char in string_in:
    if char not in new_string:
      new_string += char
  if new_string == string_in:
    return True
  else:
    return False
print(unique_characters("aple"))
def unique_characters(string_in):
  """ Returns True if all characters in the input are unique; otherwise, False. """
  if len(string_in) == 0:
    return IOError("Invalid input")
  return len(string_in) - len(set(string_in)) == 0

This is not the most efficient solution for the average case, but it is a very simple solution. The most efficient solution would be to use the set() data structure to track all letters as you iterate through the string. When you first encounter a repeated string, you break and return False. This would reduce the average and best case time complexity, but would not reduce the worse case time complexity as you still need to check each letter. The more efficient solution would be:

def unique_characters(string_in):
  if len(string_in) == 0:
    return IOError("Invalid input")
  char_set = set([])
  for char in string_in:
    if char in char_set:
      return False
    else:
      char_set.add(char)
  return True