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
Question
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
Solution
To solve this problem, we can use a data structure called a LinkedHashMap. This data structure maintains the order of insertion of elements and also allows us to efficiently check if an element already exists in the map.
Here are the steps to find the first non-repeating character each time a character is inserted into the stream:
- Initialize an empty LinkedHashMap to store the characters and their counts.
- For each character in the stream: a. If the character is already present in the LinkedHashMap, increment its count. b. If the character is not present in the LinkedHashMap, add it with a count of 1.
- For each character in the stream: a. If the count of the character in the LinkedHashMap is 1, print the character as the first non-repeating character. b. If the count of the character in the LinkedHashMap is greater than 1, continue to the next character. c. If the character is not present in the LinkedHashMap, print -1.
Let's implement this algorithm in code:
from collections import OrderedDict
def find_first_non_repeating(stream):
char_counts = OrderedDict()
for char in stream:
if char in char_counts:
char_counts[char] += 1
else:
char_counts[char] = 1
for char in stream:
if char_counts[char] == 1:
return char
return -1
# Example usage
stream = "aabc"
result = find_first_non_repeating(stream)
print(result) # Output: a
In the above code, we use an OrderedDict from the collections module to maintain the order of insertion of characters. We iterate over the stream twice - once to count the occurrences of each character and once to find the first non-repeating character. The time complexity of this algorithm is O(n), where n is the length of the stream.
Similar Questions
First non repeating character
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
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
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".
Problem StatementDurga, a computer science student, wants to write a program that inputs a character and determines its predecessor according to two rules: If the character is 'a' or 'A', map it to 'z' or 'Z'For any other character, map it to the one immediately before it. Help her with the program that uses a function called processInput() and prints the immediate predecessor of the input character.Input format :The input character is a valid alphabetical character (either uppercase or lowercase).Output format :The output displays a single character, which is the predecessor of the input character based on the specified rules.Refer to the sample output for formatting specifications.Code constraints :The character is a valid alphabetical character (either uppercase or lowercase).The program does not handle non-alphabetic characters.Sample test cases :Input 1 :AOutput 1 :ZInput 2 :gOutput 2 :fInput 3 :zOutput 3 :yInput 4 :tOutput 4 :sNote :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.Marks : 10Negative Marks : 0
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.