O(N) time O(1) space 29ms

  • 0
    public class Solution {
        public int firstUniqChar(String s) {
            int l=s.length();
            if(l==0)  return -1;
            if(l==1) return 0;
            for(int i=0;i<l;i++){
                char c=s.charAt(i);
                if(i<l-1) {
                if(s.indexOf(c,i+1)==-1 && s.indexOf(c)==i) return i;
                    if( s.indexOf(c)==i) return i;
            return -1;

  • 0

    I believe this is O(n^2). The indexOf() function has to iterate through the entire string to search for the character.

Log in to reply

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