Sunday, September 4, 2016

Solution to Leetcode 387. : First Unique Character in a String

Following is the link for problem statement
https://leetcode.com/problems/first-unique-character-in-a-string/

Solution:

class Solution {
public:
    int firstUniqChar(string s) {
        int valArray[26] = {0};
       
       
        for(int i=0;i<s.size();i++){
            valArray[s.at(i) - 'a'] ++;
        }
        int i=0;
        for(;i<s.size();i++){
            if (valArray[s.at(i) - 'a']  == 1){
               return i;
            }
        }
        return -1;
    }
};

Status : Accepted

No comments:

Post a Comment