Advertisement
imashutosh51

First non-repeating character in a stream

Aug 10th, 2022
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 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. class Solution {
  10.     public:
  11.         string FirstNonRepeating(string s){
  12.             string res="";
  13.             unordered_map<char,int> m;
  14.             queue <char> q;
  15.             for(int i=0;i<s.size();i++){
  16.                 char itr=s[i];
  17.                 if(m[itr])
  18.                    m[itr]++;
  19.                 else{
  20.                    m[itr]=1;
  21.                     q.push(itr);
  22.                 }
  23.                 while(!q.empty()){
  24.                     if(m[q.front()]==1)
  25.                        break;
  26.                     q.pop();
  27.                 }
  28.                 if(q.empty())
  29.                    res+='#';
  30.                 else
  31.                    res+=q.front();
  32.             }
  33.             return res;
  34.         }
  35.  
  36. };
  37.  
  38. /*
  39. *DLL will contain the non-repeating characters in order of stream,and head of
  40.  DLL contain first non-repeating character.
  41.  
  42. *We are also maintining two array:
  43.   repeated:It keeps track of characters visited more than one times.
  44.   indll: It keeps address of node of characters present in DLL.
  45. *Iterate through string and do following steps:
  46.    i)If repeated[current_char] is true, ignore this character.
  47.      because current_char is already repeated two or more times in the stream.
  48.    ii)If repeated[current_char] is false and inDLL[current_char] is NULL means
  49.       current_char is seen the first time then Append current_char to DLL and
  50.       store address of new DLL node in inDLL[currnt_char].
  51.    iii)If repeated[current_Char] is false and inDLL[x] is not NULL current_char is seen
  52.        a second time then Get DLL node of current_char using inDLL[current_char]
  53.        and remove the node. Also, mark inDLL[current_char] as NULL and
  54.        repeated[current_char] as true.
  55. */
  56.  
  57. struct Node{
  58.     char data;
  59.     Node *prev,*next;
  60.     Node(char k){
  61.         data=k;
  62.         prev=NULL;
  63.         next=NULL;
  64.     }
  65. };
  66.  
  67. class Solution {
  68.     public:
  69.         string FirstNonRepeating(string s){
  70.             bool repeated[256];
  71.             Node * indll[256];
  72.             memset(repeated,false,sizeof(repeated));
  73.             memset(indll,NULL,sizeof(indll));
  74.             string ans="";
  75.             Node *head=NULL,*tail=NULL;
  76.            
  77.             for(int i=0;i<s.size();i++){
  78.                 int k=s[i];
  79.                 if(repeated[k]==false){
  80.                     if(indll[k]==NULL){  //if character is not in dll
  81.                         Node *temp=new Node(s[i]);
  82.                         temp->prev=NULL;
  83.                         temp->next=NULL;
  84.                         if(!head){
  85.                             head=tail=temp;
  86.                         }
  87.                         else{
  88.                             tail->next=temp;
  89.                             temp->prev=tail;
  90.                             tail=temp;
  91.                         }
  92.                         indll[k]=temp;
  93.                     }
  94.                     else{   //if character is in dll,then remove that
  95.                         repeated[k]=true;
  96.                         if(head==NULL){indll[k]=NULL;continue;}
  97.                         if(head==indll[k]){head=head->next;if(head)head->prev=NULL;}
  98.                         else if(tail==indll[k]){tail=tail->prev;if(tail)tail->next=NULL;}
  99.                         else{
  100.                              Node *p=indll[k]->prev;
  101.                              Node *n=indll[k]->next;
  102.                              p->next=n;
  103.                              n->prev=p;
  104.                         }
  105.                         indll[k]=NULL;
  106.                     }
  107.                 }
  108.                if(head)ans+=head->data;
  109.                else ans+="#";
  110.                
  111.             }
  112.             return ans;
  113.         }
  114.  
  115. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement