Knowee
Questions
Features
Study Tools

Find the First Non-Repeating CharacterLara is reading a novel and she wants to identify the first character in the text that does not repeat. Write a program to help Lara find that character.Constraints:NAExample:Sample Input;swissSample Output:wExplanation:In the above example, w is the first character which does not repeat again in the word swissPublic Test Cases:# INPUT EXPECTED OUTPUT1 swissw

Question

Find the First Non-Repeating CharacterLara is reading a novel and she wants to identify the first character in the text that does not repeat. Write a program to help Lara find that character.Constraints:NAExample:Sample Input;swissSample Output:wExplanation:In the above example, w is the first character which does not repeat again in the word swissPublic Test Cases:# INPUT EXPECTED OUTPUT1 swissw

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a step-by-step solution to find the first non-repeating character in a string:

  1. Initialize an empty dictionary. This will be used to store the frequency of each character in the string.

  2. Iterate over the string. For each character, if it is already in the dictionary, increment its frequency by 1. If it is not in the dictionary, add it with a frequency of 1.

  3. Iterate over the string a second time. For each character, check its frequency in the dictionary. If the frequency is 1, return this character as it is the first non-repeating character.

  4. If no non-repeating character is found, return an appropriate message.

Here is a Python code snippet that implements the above steps:

def find_first_non_repeating_char(s):
    frequency = {}
    for char in s:
        if char in frequency:
            frequency[char] += 1
        else:
            frequency[char] = 1

    for char in s:
        if frequency[char] == 1:
            return char

    return "No non-repeating character found"

print(find_first_non_repeating_char('swiss'))  # Output: 'w'

In the given example, 'swiss', 'w' is the first character which does not repeat again in the word 'swiss'. Hence, the output is 'w'.

This problem has been solved

Similar Questions

First non repeating character

Given a stream of characters and we have to write a program to find first non repeating character each time a character is inserted to the stream.Input:a a b cOutput:a -1 b bExplanation: When a is added to the stream, there is no other character before it, so a is the first non-repeating character. When a a is the stream, there are 0 characters that are non-repeating, hence the output is -1. When b is added to the stream, b is the first non-repeating character, hence the output is b and so on.Input: a a cOutput: a -1 cNote: For empty strings(Single space), print -1

Take the string input from the user, it should return the first recurring character in it, or "null" if there is no recurring characterFor example, given the string "acbbac" , return "b". Given the string "abcdef", return "null".

Given a string of characters, find the first repeating character.Input FormatThe first line of input contains T - the number of test cases. It's followed by T lines, each line contains a single string of characters.Output FormatFor each test case, print the first repeating character, separated by a new line. If there are none, print '.'.Constraints1 <= T <= 1000'a' <= str[i] <= 'z'1 <= len(str) <= 104ExampleInput4datastructuresalgorithmssmartinterviewshackerrankOutputa.sa

Given a string of characters, find the first repeating character.Input FormatThe first line of input contains T - the number of test cases. It's followed by T lines, each line contains a single string of characters.Output FormatFor each test case, print the first repeating character, separated by a new line. If there are none, print '.'.Constraints1 <= T <= 1000'a' <= str[i] <= 'z'1 <= len(str) <= 104ExampleInput4datastructuresalgorithmssmartinterviewshackerrankOutputa.tr

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.