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 () 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 () below!
You can also find further discussion and get answers to your questions over in #get-help.
Agree with a comment or answer? Like () to up-vote the contribution!
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
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"))
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: 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"))
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