imashutosh51

First non-repeating character in a stream

Aug 10th, 2022 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.49 KB | None | 0 0
  1. '''
  2. Logic:
  3.  queue consists only the characters which is non-repeated and also in order.after
  4.  inserting every character,we go through queue and pop elements whose frequency is
  5.  more than 1.
  6. Time Complexity : O(n) because for loop also runs for whole array and while loop
  7.   also runs for whole array in worst case
  8. '''
  9. from collections import deque
  10. class Solution:
  11.     def firstNonRepeating(self, s):
  12.         cur_dict={}
  13.         q=deque([])
  14.         ans=''
  15.         for i in range(len(s)):
  16.             if s[i] in cur_dict.keys():
  17.                 cur_dict[s[i]]+=1
  18.             else:
  19.                 q.append(s[i])
  20.                 cur_dict[s[i]]=1
  21.             while q and cur_dict.get(q[0],0)>1:
  22.                     q.popleft()
  23.             if len(q)>0:
  24.                 ans+=q[0]
  25.             else:
  26.                 ans+='#'
  27.         return ans
  28.      
  29. '''
  30. *DLL will contain the non-repeating characters in order of stream,and head of
  31. DLL contain first non-repeating character.
  32.  
  33. *We are also maintining two array:
  34.  repeated:It keeps track of characters visited more than one times.
  35.  indll: It keeps address of node of characters present in DLL.
  36. *Iterate through string and do following steps:
  37.   i)If repeated[current_char] is true, ignore this character.
  38.     because current_char is already repeated two or more times in the stream.
  39.   ii)If repeated[current_char] is false and inDLL[current_char] is NULL means
  40.      current_char is seen the first time then Append current_char to DLL and
  41.      store address of new DLL node in inDLL[currnt_char].
  42.   iii)If repeated[current_Char] is false and inDLL[x] is not NULL current_char is seen
  43.       a second time then Get DLL node of current_char using inDLL[current_char]
  44.       and remove the node. Also, mark inDLL[current_char] as NULL and
  45.       repeated[current_char] as true.
  46. '''
  47.  
  48. struct Node{
  49.     char data;
  50.     Node *prev,*next;
  51.     Node(char k){
  52.         data=k;
  53.         prev=NULL;
  54.         next=NULL;
  55.     }
  56. };
  57.  
  58. class Solution {
  59.     public:
  60.         string FirstNonRepeating(string s){
  61.             bool repeated[256];
  62.             Node * indll[256];
  63.             memset(repeated,false,sizeof(repeated));
  64.             memset(indll,NULL,sizeof(indll));
  65.             string ans="";
  66.             Node *head=NULL,*tail=NULL;
  67.            
  68.             for(int i=0;i<s.size();i++){
  69.                 int k=s[i];
  70.                 if(repeated[k]==false){
  71.                     if(indll[k]==NULL){  //if character is not in dll
  72.                         Node *temp=new Node(s[i]);
  73.                         temp->prev=NULL;
  74.                         temp->next=NULL;
  75.                         if(!head){
  76.                             head=tail=temp;
  77.                         }
  78.                         else{
  79.                             tail->next=temp;
  80.                             temp->prev=tail;
  81.                             tail=temp;
  82.                         }
  83.                         indll[k]=temp;
  84.                     }
  85.                     else{   //if character is in dll,then remove that
  86.                         repeated[k]=true;
  87.                         if(head==NULL){indll[k]=NULL;continue;}
  88.                         if(head==indll[k]){head=head->next;if(head)head->prev=NULL;}
  89.                         else if(tail==indll[k]){tail=tail->prev;if(tail)tail->next=NULL;}
  90.                         else{
  91.                              Node *p=indll[k]->prev;
  92.                              Node *n=indll[k]->next;
  93.                              p->next=n;
  94.                              n->prev=p;
  95.                         }
  96.                         indll[k]=NULL;
  97.                     }
  98.                 }
  99.                if(head)ans+=head->data;
  100.                else ans+="#";
  101.                
  102.             }
  103.             return ans;
  104.         }
  105.  
  106. };
Advertisement
Add Comment
Please, Sign In to add comment