Dr Gomathi
Write a Python function that finds the first non-repeating character in a given string. If every character repeats, return None. The function should be case-sensitive, meaning 'A' and 'a' should be considered different characters.
By Dr Gomathi in Python on Jan 19 2024
  • Ganesh Kavhar
    Jan, 2024 23

    please find below solution
    `string = “hello”
    index = -1
    fnc = “”

    if len(string) == 0 :
    print(“EMTPY STRING”);

    for i in string:
    if string.count(i) == 1:
    fnc += i
    break
    else:
    index += 1
    if index == len(string)-1 :
    print(“All characters are repeating “)
    else:
    print(“First non-repeating character is”, fnc)`

    • 0
  • Manikandan Murugesan
    Jan, 2024 22

    def first_non_repeating_character(input_string):char_count = {}# Count the occurrences of each character in the stringfor char in input_string:char_count[char] = char_count.get(char, 0) 1# Find the first non-repeating characterfor char in input_string:if char_count[char] == 1:return char# If every character repeats, return Nonereturn None# Example usage: input_str = "abacabad" result = first_non_repeating_character(input_str)if result is not None:print(f"The first non-repeating character is: {result}") else:print("Every character repeats.")

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS