Java O(n^2) time and O(1) space solution


  • 0
    S
    public int firstUniqChar(String s) {
    		if(s.length()==1) {
    			return 0;
    		}else {
    			for(int i=0;i<s.length();i++) {
    				if((s.indexOf(s.charAt(i))==i)&&(s.indexOf(s.charAt(i),i+1)==-1)) {
    					return i;
    				}
    			}
    			return -1;
    		}
        }
    

  • 0
    K

    It's not an O(n) it's O(n^2) solution.


Log in to reply
 

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