📌  相关文章
📜  使用链表查找给定字符串中的第一个非重复字符

📅  最后修改于: 2021-09-03 03:53:31             🧑  作者: Mango

给定一个长度为L的字符串str ,任务是找到该字符串第一次出现的非重复字符。

例子:

注意: HashMap 方法请参考这篇文章,这篇文章是空间优化方法。

链表方法:想法是使用链表来跟踪字符串中的唯一元素。下面是该方法的说明:

  • 遍历字符串的字符串中的每个字符,并在链接列表中添加字符上的以下条件的基础:
    • 如果该字符已存在于链表中,则从链表中删除现有字符节点。
    • 否则,将该字符添加到链表中。
  • 最后,在该链接的表的第一个节点的字符为所述字符串的所述第一非重复的字符。

下面是上述方法的实现:

C++14
// C++ implementation to find the
// first non-repeating element of the
// string using Linked List
#include 
using namespace std;
 
// Function to find the first
// non-repeating element of the
// given string using Linked List
void firstNonRepElement(string str)
{
    map mpp;
 
    for(auto i:str)
    {
        mpp[i]++;
    }
 
    for(auto i:str)
    {
        if (mpp[i] == 1)
        {
            cout << i << endl;
            return;
        }
    }
    return;
}
 
// Driver Code
int main()
{
    string str = "geeksforgeeks";
 
    // Function Call
    firstNonRepElement(str);
}
 
// This code is contributed by mohit kumar 29


Java
// Java implementation to find the
// first non-repeating element
// of the string using Linked List
 
import java.util.LinkedList;
 
public class FirstNonRepeatingElement {
 
    // Function to find the first
    // non-repeating element of the
    // given string using Linked List
    static void firstNonRepElement(String str)
    {
        LinkedList list
            = new LinkedList();
 
        list.add(str.charAt(0));
 
        for (int i = 1; i < str.length(); i++) {
            if (list.contains(str.charAt(i)))
                list.remove(list.indexOf(
                    str.charAt(i)));
            else
                list.add(str.charAt(i));
        }
        System.out.println(list.get(0));
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String str = "geeksforgeeks";
 
        // Function Call
        firstNonRepElement(str);
    }
}


Python3
# Python3 implementation to find the
# first non-repeating element of the
# string using Linked List
import collections
 
# Function to find the first
# non-repeating element of the
# given string using Linked List
def firstNonRepElement(str):
     
    # Make list as a linkedlist
    list = collections.deque()# linkedlist
 
    list.append(str[0])
 
    for i in range(len(str)):
        if str[i] in list:
            list.remove(str[i])
        else:
            list.append(str[i])
             
    print(list[0])
 
# Driver Code
if __name__=='__main__':
     
    str = "geeksforgeeks";
     
    # Function Call
    firstNonRepElement(str);
 
# This code is contributed by pratham76


C#
// C# implementation to find the
// first non-repeating element
// of the string using Linked List
using System;
using System.Collections;
using System.Collections.Generic;
 
class FirstNonRepeatingElement{
 
// Function to find the first
// non-repeating element of the
// given string using Linked List
static void firstNonRepElement(string str)
{
    LinkedList list = new LinkedList();
 
    list.AddLast(str[0]);
 
    for(int i = 1; i < str.Length; i++)
    {
        if (list.Contains(str[i]))
            list.Remove(str[i]);
        else
            list.AddLast(str[i]);
    }
    Console.Write(list.First.Value);
}
 
// Driver Code
public static void Main(string[] args)
{
    string str = "geeksforgeeks";
 
    // Function call
    firstNonRepElement(str);
}
}
 
// This code is contributed by rutvik_56


输出
f

性能分析:

  • 时间复杂度: O(N * 26)
  • 辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live