Find the unique alphabet in a string and find the index of that character If multiple unique chars are in a given string, then return the first unique character index,if there is no unique alphabet then print -1
Note: index format is 1
For example :
given string: statistics
Given string having a, c as unique chars
The first unique char is a
character an index in the given string is 3 (index is 1 format)
output: 3
For example : "helloram"
unique chars = [h,e,o,r,a,m]
first one out of them is "h
N = input("Enter word: ")
def first_unique_character(str1):
char_order = []
ctr = {}
for c in str1:
if c in ctr:
ctr[c] += 1
else:
ctr[c] = 1
char_order.append(c)
for c in char_order:
if ctr[c] == 1:
return c
return char_order
letter = list(first_unique_character(N))
if len(letter) == 1:
print(N.find(letter[0]) + 1)
else:
print('-1')
Comments
Leave a comment