# Print first K non-repeating characters of a string Heap

Hard

### Problem Statement :

Given a string `str`, find first `K` non-repeating characters in lexicographical order, if all the characters are repeating then print "Not Exist" without quotes.

See original problem statement here

### Solution Approach :

#### Introduction :

We need to print first `K` characters in lexicographical order. We can use min-heap, each time we extract a character from our heap, we check the occurence of the character, if it is `1` then we will accept the character otherwise reject it.

#### Method 1 :

We can sort first `k` characters of the string using any sorting algorithm, now store the frequency of each character of the string. Now take the first `k` characters which are not repeated and print them.

#### Method 2 :

We need to geneate a min heap of characters of size `k`.
In a min-heap, the root always stores the smaller value as compared to its left & right subtree, this condition needs to be true for every node. We need to insert each item one by one such that it's parent is always smaller than the item itself. If the parent is greater, then we need to swap the current item with its parent.

We will insert first `k` characters from `str` which are non repeating, in the heap. We can keep track of the frequency of each character in `str` using frequency array. Once we are done creating heap for first `k` non repeating characters, we will extract all characters one-by-one and print it,
if all the characters in `str` are repeating print "Not Exist" without quotes.

extract(): Removes the minimum element from Min-Heap. Time Complexity of this Operation is O(Logn) as this operation needs to maintain the heap property (by calling heapify()) after removing root.

heapify(): Maintains the heap property for each node. If any node does not follow heap property it swaps the node with the node which is smaller ,or greater (in case of max-heap), than the node.

### Algorithms :

#### Insert():

1. Insert the item at the last index, and increment the size by 1.
2. Then, check if the inserted item is smaller than its parent,
3. If yes, then swap the inserted item with its parent.
4. If no, then do nothing.
5. Now, go to step `2` and repeat untill we reach root (first element).

#### Extract() :

1. Store the value of the first node of our heap ( `temp = heap` ).
2. Replace the root node with the farthest right node (last element).
3. Decrease the size by `1`. `(heap = heap[size-1])`
4. Perform heapify starting from the new root.
5. Return the stored value.(temp)

#### Heapify():

1. if the heap property holds true then you are done.
2. else if
3. the replacement node value <= its parent nodes value
then swap them, and repeat step 3.
4. else
5. swap the replacement node with the smallest child node, and
repeat step 3.

#### Example:  ### Solutions:

```#include<string.h>
#include<stdlib.h>

int parent(int i)
{
return (i-1)/2;
}

int left(int i)
{
return (i*2)+1;
}

int right(int i)
{
return (i*2)+2;
}

void swap(char *a,char *b)
{
char temp = *a;
*a = *b;
*b = temp;
}

void insert(char heap[],int *size,char val)
{
heap[*size] = val;
int i = *size;
(*size)++;

while(i!=0 && heap[parent(i)]> heap[i])
{
swap(&heap[parent(i)],&heap[i]);
i = parent(i);
}
}
void heapify(char heap[],int i,int size)
{
int l = left(i);
int r = right(i);
int s = i;
if(l<size && heap[l]<heap[i])
s = l;
if(r<size && heap[r]<heap[s])
s = r;

if(s!=i)
{
swap(&heap[i],&heap[s]);
heapify(heap,s,size);
}
}

char extract(char heap[], int *size)
{
char root = heap;
heap= heap[(*size)-1];
(*size)--;
heapify(heap,0,*size);

return root;
}

int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int k;
char ch;
scanf("%s",&ch);
k = atoi(ch);
//cout<<k<<endl;
int hash={0};
fflush(stdin);
char str;
scanf("%s",&str);
fflush(stdin);
int n = strlen(str);
str[n] ='\0';
char *heap = (char*)calloc(sizeof(char),(k+1));
int size = 0;

for(int i=0;i<n;i++)
{
hash[str[i]-'a']++;
}

int count = 1;
for(int i=0;i<n;i++)
{
if(hash[str[i]-'a']==1 && count<=k )
{
insert(heap,&size,str[i]);
count++;
}
}

if(count==1)
{
printf("%s\n","Not Exist");
continue;
}
int c = size;
while(c--)
{
printf("%c",extract(heap,&size));
// printf(" %d ", n);
}
printf("\n");
}
return 0;
}
```
```#include<bits/stdc++.h>
using namespace std;
int parent(int i)
{
return (i-1)/2;
}
int left(int i)
{
return (i*2)+1;
}
int right(int i)
{
return (i*2)+2;
}
void swap(char *a,char *b)
{
char temp = *a;
*a = *b;
*b = temp;
}
void insert(char heap[],int *size,char val)
{
heap[*size] = val;
int i = *size;
(*size)++;
while(i!=0 && heap[parent(i)]> heap[i])
{
swap(&heap[parent(i)],&heap[i]);
i = parent(i);
}
}
void heapify(char heap[],int i,int size)
{
int l = left(i);
int r = right(i);
int s = i;
if(l<size && heap[l]<heap[i])
s = l;
if(r<size && heap[r]<heap[s])
s = r;
if(s!=i)
{
swap(&heap[i],&heap[s]);
heapify(heap,s,size);
}
}
char extract(char heap[], int *size)
{
char root = heap;
heap= heap[(*size)-1];
(*size)--;
heapify(heap,0,*size);
return root;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int k;
cin>>k;
char hash={0};
string str;
cin>>str;
int n = str.length();
char *heap = (char*)malloc(sizeof(char)*(k+1));
int size = 0;
for(int i=0;i<n;i++)
{
hash[str[i]-'a']++;
}

int count = 1;
for(int i=0;i<n;i++)
{
if(hash[str[i]-'a']==1 && count<=k )
{
insert(heap,&size,str[i]);
count++;
}
}

if(count==1)
{
cout<<"Not Exist"<<endl;
continue;
}
int c = size;
while(c--)
{
cout<<extract(heap,&size);
// printf(" %d ", n);
}
cout<<endl;
}
return 0;
}
```

[forminator_quiz id="1540"]

This article tried to discuss Heap. Hope this blog helps you understand and solve the problem. To practice more problems on heap you can check out MYCODE | Competitive Programming.