📌  相关文章
📜  从字符流找到的第一个非重复的字符

📅  最后修改于: 2021-10-27 08:10:50             🧑  作者: Mango

给定一个字符流,从流中找到第一个非重复字符。您需要随时在 O(1) 时间内告诉第一个非重复字符。

如果我们遵循这里讨论的第一种方法,那么我们需要存储流,以便我们可以在任何时候再次遍历它以找到第一个非重复字符。如果我们使用同一篇文章中讨论的扩展方法,每次查询第一个非重复元素时,我们都需要遍历计数数组。我们可以随时从流中找到第一个非重复字符,而无需遍历任何数组。

该想法是使用一个DLL(d oubly大号着墨大号IST)以有效地获得从流的第一个非重复的字符。 DLL 按顺序包含所有非重复字符,即DLL 的头部包含第一个非重复字符,第二个节点包含第二个非重复字符,依此类推。

我们还维护了两个数组:一个数组是维护已经被访问过两次或多次的字符,我们称之为repeat[],另一个数组是一个指向链表节点的指针数组,我们称之为inDLL[]。两个数组的大小等于字母表大小,通常为 256。

  1. 创建一个空的 DLL。还要创建两个大小为 256 的数组 inDLL[] 和重复的 []。 inDLL 是指向 DLL 节点的指针数组。 repeat[] 是一个布尔数组,如果 x 重复两次或多次,repeat[x] 为真,否则为假。如果 DLL 中存在字符x,则 inDLL[x] 包含指向 DLL 节点的指针,否则为 NULL。
  2. 将 inDLL[] 的所有条目初始化为 NULL,并将重复 [] 初始化为 false。
  3. 要获取第一个非重复字符,请返回 DLL 开头的字符。
  4. 以下是处理流中新字符“x”的步骤。
    • 如果repeat[x] 为真,忽略这个字符(x 已经在流中重复了两次或更多次)
    • 如果repeat[x] 为false 并且inDLL[x] 为NULL(第一次看到x)。将 x 附加到 DLL 并将新 DLL 节点的地址存储在 inDLL[x] 中。
    • 如果repeat[x] 为false 并且inDLL[x] 不是NULL(第二次看到x)。使用 inDLL[x] 获取 x 的 DLL 节点并删除该节点。此外,将 inDLL[x] 标记为 NULL 并将重复 [x] 标记为 true。

请注意,如果我们维护尾指针,将新节点附加到 DLL 是O(1)操作。从 DLL 中删除节点也是O(1) 。因此,添加新字符和查找第一个非重复字符这两个操作都需要O(1)时间。

下图是上述方法的试运行:

下面是上述方法的实现:

C++
// A C++ program to find first
// non-repeating character
// from a stream of characters
#include 
#define MAX_CHAR 256
using namespace std;
 
// A linked list node
struct node {
    char a;
    struct node *next, *prev;
};
 
// A utility function to append a character x at the end
// of DLL. Note that the function may change head and tail
// pointers, that is why pointers to these pointers are
// passed.
void appendNode(struct node** head_ref,
                struct node** tail_ref, char x)
{
    struct node* temp = new node;
    temp->a = x;
    temp->prev = temp->next = NULL;
 
    if (*head_ref == NULL) {
        *head_ref = *tail_ref = temp;
        return;
    }
    (*tail_ref)->next = temp;
    temp->prev = *tail_ref;
    *tail_ref = temp;
}
 
// A utility function to remove a node 'temp' fromt DLL.
// Note that the function may change head and tail pointers,
// that is why pointers to these pointers are passed.
void removeNode(struct node** head_ref,
                struct node** tail_ref, struct node* temp)
{
    if (*head_ref == NULL)
        return;
 
    if (*head_ref == temp)
        *head_ref = (*head_ref)->next;
    if (*tail_ref == temp)
        *tail_ref = (*tail_ref)->prev;
    if (temp->next != NULL)
        temp->next->prev = temp->prev;
    if (temp->prev != NULL)
        temp->prev->next = temp->next;
 
    delete (temp);
}
 
void findFirstNonRepeating()
{
    // inDLL[x] contains pointer to
    // a DLL node if x is present
    // in DLL. If x is not present, then inDLL[x] is NULL
    struct node* inDLL[MAX_CHAR];
 
    // repeated[x] is true if x is repeated two or more
    // times. If x is not seen so far or x is seen only
    // once. then repeated[x] is false
    bool repeated[MAX_CHAR];
 
    // Initialize the above two arrays
    struct node *head = NULL, *tail = NULL;
    for (int i = 0; i < MAX_CHAR; i++) {
        inDLL[i] = NULL;
        repeated[i] = false;
    }
 
    // Let us consider following stream and see the process
    char stream[] = "geeksforgeeksandgeeksquizfor";
    for (int i = 0; stream[i]; i++) {
        char x = stream[i];
        cout << "Reading " << x << " from stream \n";
 
        // We process this character only if it has not
        // occurred or occurred only once. repeated[x] is
        // true if x is repeated twice or more.s
        if (!repeated[x]) {
            // If the character is not in DLL, then add this
            // at the end of DLL.
            if (inDLL[x] == NULL) {
                appendNode(&head, &tail, stream[i]);
                inDLL[x] = tail;
            }
            else // Otherwise remove this character from DLL
            {
                removeNode(&head, &tail, inDLL[x]);
                inDLL[x] = NULL;
                repeated[x]
                    = true; // Also mark it as repeated
            }
        }
 
        // Print the current first non-repeating character
        // from stream
        if (head != NULL)
            cout << "First non-repeating character so far "
                    "is "
                 << head->a << endl;
    }
}
 
/* Driver code */
int main()
{
    findFirstNonRepeating();
    return 0;
}


Java
// A Java program to find first non-repeating character
// from a stream of characters
 
import java.util.ArrayList;
import java.util.List;
 
public class NonReapeatingC {
    final static int MAX_CHAR = 256;
 
    static void findFirstNonRepeating()
    {
        // inDLL[x] contains pointer to a DLL node if x is
        // present in DLL. If x is not present, then
        // inDLL[x] is NULL
        List inDLL = new ArrayList();
 
        // repeated[x] is true if x is repeated two or more
        // times. If x is not seen so far or x is seen only
        // once. then repeated[x] is false
        boolean[] repeated = new boolean[MAX_CHAR];
 
        // Let us consider following stream and see the
        // process
        String stream = "geeksforgeeksandgeeksquizfor";
        for (int i = 0; i < stream.length(); i++) {
            char x = stream.charAt(i);
            System.out.println("Reading " + x
                               + " from stream \n");
 
            // We process this character only if it has not
            // occurred or occurred only once. repeated[x]
            // is true if x is repeated twice or more.s
            if (!repeated[x]) {
                // If the character is not in DLL, then add
                // this at the end of DLL.
                if (!(inDLL.contains(x))) {
                    inDLL.add(x);
                }
                else // Otherwise remove this character from
                     // DLL
                {
                    inDLL.remove((Character)x);
                    repeated[x]
                        = true; // Also mark it as repeated
                }
            }
 
            // Print the current first non-repeating
            // character from stream
            if (inDLL.size() != 0) {
                System.out.print(
                    "First non-repeating character so far is ");
                System.out.println(inDLL.get(0));
            }
        }
    }
 
    /* Driver code */
    public static void main(String[] args)
    {
        findFirstNonRepeating();
    }
}
// This code is contributed by Sumit Ghosh


Python3
# A Python program to find first non-repeating character from
# a stream of characters
MAX_CHAR = 256
 
def findFirstNonRepeating():
 
    # inDLL[x] contains pointer to a DLL node if x is present
    # in DLL. If x is not present, then inDLL[x] is NULL
    inDLL = [] * MAX_CHAR
 
    # repeated[x] is true if x is repeated two or more times.
    # If x is not seen so far or x is seen only once. then
    # repeated[x] is false
    repeated = [False] * MAX_CHAR
 
    # Let us consider following stream and see the process
    stream = "geekforgeekandgeeksandquizfor"
    for i in range(len(stream)):
        x = stream[i]
        print ("Reading " + x + " from stream")
 
        # We process this character only if it has not occurred
        # or occurred only once. repeated[x] is true if x is
        # repeated twice or more.s
        if not repeated[ord(x)]:
 
            # If the character is not in DLL, then add this
            # at the end of DLL
            if not x in inDLL:
                inDLL.append(x)
            else:
                inDLL.remove(x)
                repeated[ord(x)] = True
 
        if len(inDLL) != 0:
            print ("First non-repeating character so far is ")
            print (str(inDLL[0]))
 
# Driver program
findFirstNonRepeating()
 
# This code is contributed by BHAVYA JAIN


C#
// A C# program to find first non-repeating character
// from a stream of characters
using System;
using System.Collections.Generic;
 
public class NonReapeatingC {
    readonly static int MAX_CHAR = 256;
 
    static void findFirstNonRepeating()
    {
        // inDLL[x] contains pointer to a DLL node if x is present
        // in DLL. If x is not present, then inDLL[x] is NULL
        List inDLL = new List();
 
        // repeated[x] is true if x is repeated two or more times.
        // If x is not seen so far or x is seen only once. then
        // repeated[x] is false
        bool[] repeated = new bool[MAX_CHAR];
 
        // Let us consider following stream and see the process
        String stream = "geeksforgeeksandgeeksquizfor";
        for (int i = 0; i < stream.Length; i++) {
            char x = stream[i];
            Console.WriteLine("Reading " + x + " from stream \n");
 
            // We process this character only if it has not occurred
            // or occurred only once. repeated[x] is true if x is
            // repeated twice or more.s
            if (!repeated[x]) {
                // If the character is not in DLL, then add this at
                // the end of DLL.
                if (!(inDLL.Contains(x))) {
                    inDLL.Add(x);
                }
                else // Otherwise remove this character from DLL
                {
                    inDLL.Remove((char)x);
                    repeated[x] = true; // Also mark it as repeated
                }
            }
 
            // Print the current first non-repeating character from
            // stream
            if (inDLL.Count != 0) {
                Console.Write("First non-repeating character so far is ");
                Console.WriteLine(inDLL[0]);
            }
        }
    }
 
    /* Driver code*/
    public static void Main(String[] args)
    {
        findFirstNonRepeating();
    }
}
 
// This code has been contributed by 29AjayKumar


Javascript


输出:

Reading g from stream
First non-repeating character so far is g
Reading e from stream
First non-repeating character so far is g
Reading e from stream
First non-repeating character so far is g
Reading k from stream
First non-repeating character so far is g
Reading s from stream
First non-repeating character so far is g
Reading f from stream
First non-repeating character so far is g
Reading o from stream
First non-repeating character so far is g
Reading r from stream
First non-repeating character so far is g
Reading g from stream
First non-repeating character so far is k
Reading e from stream
First non-repeating character so far is k
Reading e from stream
First non-repeating character so far is k
Reading k from stream
First non-repeating character so far is s
Reading s from stream
First non-repeating character so far is f
Reading a from stream
First non-repeating character so far is f
Reading n from stream
First non-repeating character so far is f
Reading d from stream
First non-repeating character so far is f
Reading g from stream
First non-repeating character so far is f
Reading e from stream
First non-repeating character so far is f
Reading e from stream
First non-repeating character so far is f
Reading k from stream
First non-repeating character so far is f
Reading s from stream
First non-repeating character so far is f
Reading q from stream
First non-repeating character so far is f
Reading u from stream
First non-repeating character so far is f
Reading i from stream
First non-repeating character so far is f
Reading z from stream
First non-repeating character so far is f
Reading f from stream
First non-repeating character so far is o
Reading o from stream
First non-repeating character so far is r
Reading r from stream
First non-repeating character so far is a

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程