Find the first non-repeating character from a stream of characters
2015-02-02 23:34
627 查看
转自:http://www.geeksforgeeks.org/find-first-non-repeating-character-stream-characters/
Given a stream of characters, find the first non-repeating character from stream. You need to tell the first non-repeating character in O(1) time at any moment.
If we follow the first approach discussed here, then we need to store the stream so that we can
traverse it one more time to find the first non-repeating character at any moment. If we use extended approach discussed in the same
post, we need to go through the count array every time first non-repeating element is queried. We can find the first non-repeating character from stream at any moment without traversing any array.
We strongly recommend you to minimize the browser and try it yourself first.
The idea is to use a DLL (Doubly Linked List) to efficiently get the first non-repeating character from a stream. The DLL contains all non-repeating characters in order, i.e., the head of DLL contains first
non-repeating character, the second node contains the second non-repeating and so on.
We also maintain two arrays: one array is to maintain characters that are already visited two or more times, we call it repeated[], the other array is array of pointers to linked list nodes, we call it inDLL[]. The size of both arrays is equal to alphabet size
which is typically 256.
Note that appending a new node to DLL is O(1) operation if we maintain tail pointer. Removing a node from DLL is also O(1). So both operations, addition of new character and finding first non-repeating character take O(1) time.
Given a stream of characters, find the first non-repeating character from stream. You need to tell the first non-repeating character in O(1) time at any moment.
If we follow the first approach discussed here, then we need to store the stream so that we can
traverse it one more time to find the first non-repeating character at any moment. If we use extended approach discussed in the same
post, we need to go through the count array every time first non-repeating element is queried. We can find the first non-repeating character from stream at any moment without traversing any array.
We strongly recommend you to minimize the browser and try it yourself first.
The idea is to use a DLL (Doubly Linked List) to efficiently get the first non-repeating character from a stream. The DLL contains all non-repeating characters in order, i.e., the head of DLL contains first
non-repeating character, the second node contains the second non-repeating and so on.
We also maintain two arrays: one array is to maintain characters that are already visited two or more times, we call it repeated[], the other array is array of pointers to linked list nodes, we call it inDLL[]. The size of both arrays is equal to alphabet size
which is typically 256.
1) Create an empty DLL. Also create two arrays inDLL[] and repeated[] of size 256. inDLL is an array of pointers to DLL nodes. repeated[] is a boolean array, repeated[x] is true if x is repeated two or more times, otherwise false. inDLL[x] contains pointer to a DLL node if character x is present in DLL, otherwise NULL. 2) Initialize all entries of inDLL[] as NULL and repeated[] as false. 3) To get the first non-repeating character, return character at head of DLL. 4) Following are steps to process a new character 'x' in stream. a) If repeated[x] is true, ignore this character (x is already repeated two or more times in the stream) b) If repeated[x] is false and inDLL[x] is NULL (x is seen first time) Append x to DLL and store address of new DLL node in inDLL[x]. c) If repeated[x] is false and inDLL[x] is not NULL (x is seen second time) Get DLL node of x using inDLL[x] and remove the node. Also, mark inDLL[x] as NULL and repeated[x] as true.
Note that appending a new node to DLL is O(1) operation if we maintain tail pointer. Removing a node from DLL is also O(1). So both operations, addition of new character and finding first non-repeating character take O(1) time.
相关文章推荐
- Given a set of n symbols a size k and a combination of length k of non repeating characters from the symbol set
- Given a string, find the length of the longest substring without repeating characters
- Find the First Nonrepeated Character
- LeetCode : Given a string, find the length of the longest serial substring without repeating characters.
- Find The First Non Repeated Character In A String : Technical Interview Question
- Find the two non-repeating elements in an array of repeating elements
- Find the first repeating element in an array of integers
- Length of the longest substring without repeating characters
- from given array of n elements find the maximum element for each consecutive sub-array of k elements.
- Numeric test, find the solution of non-polynomial equation, play
- Genymotion提示To find out the cause of the problem,start the virtual device from VirtualBox
- Find the first intersection of two lists (if the two list are merged at some point)
- Reaction to 构造之法 of Software Engineering From The First Chapter toThe Fifth Chapter
- Given an array of positive and negative integers find the first subarray with zero sum
- #EPI#Find running median from a stream of integers
- 35.两链表的第一个公共结点[Find the first common node of two linked list]
- Reaction to 构造之法 of Software Engineering From The First Chapter toThe Fifth Chapter(补充版)
- the first non singleton dimension of a matrix的意思
- Find the first un-repeated character in a string(待整理)
- Length of the longest substring without repeating characters(dp)