my java solution 26ml O(n) time O(1) space


  • 0
    R
    public class Solution {
        public int firstUniqChar(String s) {
          int[] dic = new int[26];
          Arrays.fill(dic, -1);
          for(int i = 0;i<s.length();i++) {
              int index = s.charAt(i) - 'a';
              if(dic[index]==-2) continue;
              else if(dic[index]==-1)
                dic[index] = i;
              else{
                  dic[index] = -2;
              }
          }
          int ret = s.length();
          for(int i: dic){
              if(i>=0&&i<ret)
              ret = i;
          }
          return ret==s.length()?-1:ret;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.