Skip to content

Commit 28cba4e

Browse files
Create Day 5 First Unique Character in a String.cpp
1 parent fc08595 commit 28cba4e

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
PROBLEM:
2+
3+
4+
Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.
5+
6+
Examples:
7+
8+
s = "leetcode"
9+
return 0.
10+
11+
s = "loveleetcode",
12+
return 2.
13+
14+
15+
SOLUTION:
16+
17+
18+
class Solution {
19+
public:
20+
int firstUniqChar(string s) {
21+
22+
int n,i;
23+
n=s.length();
24+
25+
int a[26]={0};
26+
27+
for(i=0;i<n;i++)
28+
{
29+
a[s[i]-'a']++;
30+
}
31+
32+
for(i=0;i<n;i++)
33+
{
34+
if(a[s[i]-'a']==1)
35+
{
36+
return i;
37+
}
38+
}
39+
40+
return -1;
41+
42+
}
43+
};

0 commit comments

Comments
 (0)