Find the first non-repeated character in a string

前端 未结 21 1575
有刺的猬
有刺的猬 2020-12-06 03:53

I read of a job interview question to write some code for the following:

Write an efficient function to find the first nonrepeated character in a st

21条回答
  •  半阙折子戏
    2020-12-06 04:19

    # I came up with another solution but not efficient comment?
    str1 = "awbkkzafrocfbvwcqbb"
    list1 = []
    count = 0
    newlen = len(str1)
    find = False
    
    
    def myfun(count, str1, list1, newlen):
    
        for i in range(count, newlen):
    
            if i == 0:
    
                list1.append(str1[i])
    
            else:
                if str1[i] in list1:
                    str1 = str1.translate({ord(str1[i]): None})
                    print(str1)
                    newlen = len(str1)
                    count =0
                    i = count
                    list1.pop()
                    myfun(count,str1,list1,newlen)
                else:
                    pass
    
        if str1.find(list1[0], 1, len(str1)) != -1 :
            pass
        else:
            print(list1[0]+" is your first non repeating character")
            exit()
    
    
    myfun(count, str1, list1, newlen)
    

提交回复
热议问题