forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNon repeating.cpp
More file actions
35 lines (29 loc) · 765 Bytes
/
Non repeating.cpp
File metadata and controls
35 lines (29 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <bits/stdc++.h>
using namespace std;
#define NO_OF_CHARS 256
int firstNonRepeating(char *str)
{
pair<int, int> arr[NO_OF_CHARS];
for (int i = 0; str[i]; i++)
{
(arr[str[i]].first)++;
arr[str[i]].second = i;
}
int res = INT_MAX;
for (int i = 0; i < NO_OF_CHARS; i++)
if (arr[i].first == 1)
res = min(res, arr[i].second);
return res;
}
int main()
{
char str[] = "geeksforgeeks";
int index = firstNonRepeating(str);
if (index == INT_MAX)
printf("Either all characters are "
"repeating or string is empty");
else
printf("First non-repeating character"
" is %c", str[index]);
return 0;
}