Simple Java Solution. Very few lines of simple Java code.


  • 0
    V
    class Solution {
        public int firstUniqChar(String s) 
        {
        		for (int i = 0; i<s.length();i++)
        		{
        			if (s.indexOf(s.charAt(i), 0) == s.lastIndexOf(s.charAt(i), s.length()-1))
        			{
        				return i;
        			}
        			else
        			{
        				continue;
        			}
    
        		}
        		
        		return -1;
            
        }
    }
    
    

  • 0
    S
    public static int firstUniqChar(String s) {
            char sCharArr[] = s.toCharArray();
            for(int i=0; i<sCharArr.length; i++){
                if(s.lastIndexOf(sCharArr[i])==i && s.indexOf(sCharArr[i])==i) {
                    return i;
                }
            }
            return -1;
        }
    

Log in to reply
 

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