forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FirstUniqueCharacterInAString.cpp
55 lines (50 loc) · 1.75 KB
/
FirstUniqueCharacterInAString.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Source : https://leetcode.com/problems/first-unique-character-in-a-string/
// Author : Hao Chen
// Date : 2016-08-23
/***************************************************************************************
*
* Given a string, find the first non-repeating character in it and return it's index.
* If it doesn't exist, return -1.
*
* Examples:
*
* s = "leetcode"
* return 0.
*
* s = "loveleetcode",
* return 2.
*
* Note: You may assume the string contain only lowercase letters.
***************************************************************************************/
class Solution {
public:
int firstUniqChar(string s) {
//As the question mentioned, there only have lower case chars,
//so the MAX_CHAR can be defined as 26, but I want this algorithm be more general for all ASCII
#define MAX_CHAR 256
#define NOT_FOUND -1
#define DUPLICATION -2
// initlize all chars status to NOT_FOUND
int pos_map[MAX_CHAR];
memset(pos_map, NOT_FOUND,sizeof(pos_map));
// if it is the first time to find, set the status to its postion
// if it is the second time to find, set the status to duplication
// if it has already duplicated, do nothing
for (int i=0; i<s.size(); i++){
if ( pos_map[s[i]] >= 0 ) {
pos_map[s[i]] = DUPLICATION;
}else if ( pos_map[s[i]] == NOT_FOUND ) {
pos_map[s[i]] = i;
}
}
// find the lowest postion
int pos = INT_MAX;
for (auto item : pos_map) {
cout << item << ",";
if (item >= 0 && item < pos) {
pos = item;
}
}
return pos == INT_MAX ? -1 : pos;
}
};