Answer to Question #347476 in Python for R43

Question #347476

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



1
Expert's answer
2022-06-06T08:58:16-0400
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')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS