Skip to content

Instantly share code, notes, and snippets.

@sourabh2k15
Created December 26, 2017 14:02
Show Gist options
  • Select an option

  • Save sourabh2k15/96ac58b2eb87017b64790556b0f66851 to your computer and use it in GitHub Desktop.

Select an option

Save sourabh2k15/96ac58b2eb87017b64790556b0f66851 to your computer and use it in GitHub Desktop.
Leetcode #387 First Unique Character in a String
class Solution {
public:
int firstUniqChar(string s) {
if(s.length() == 0) return -1;
vector<int> frequency(26, -1);
for(int i = 0; i < s.length(); i++){
if(frequency[s[i] - 'a'] == -1) frequency[s[i] - 'a'] = i;
else{
frequency[s[i] - 'a'] = -2;
}
}
for(int i = 0; i < s.length(); i++){
if(frequency[s[i] - 'a'] >= 0) return i;
}
return -1;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment