Created
December 26, 2017 14:02
-
-
Save sourabh2k15/96ac58b2eb87017b64790556b0f66851 to your computer and use it in GitHub Desktop.
Leetcode #387 First Unique Character in a String
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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