public class firstNonRepeatedCharInString {
    
    public static void main(String abc[]){
    
    firstNonRepeatedCharInString f = new firstNonRepeatedCharInString();
    String checkString="bbzbccdede";
    String checkString1="bzzzzzzccde";
        String checkString2="abcdef";
        String checkString3="aabc";
            String checkString4="zzzzzzcc";
    int index=f.getIndexOfFirstNonRepeatingChar(checkString);    
    if(index!=-1)
    System.out.println("the first non repeating character in " +checkString 
+" is at "+checkString.charAt(index));
        else
    System.out.println("there is no repeating character in " +checkString 
);    
        
 index=f.getIndexOfFirstNonRepeatingChar(checkString1);    
if(index!=-1){
        System.out.println("the first non repeating character in " 
+checkString1 +" is at "+checkString1.charAt(index));
    }else
        System.out.println("there is no repeating character in " 
+checkString1 );
    
    
 index=f.getIndexOfFirstNonRepeatingChar(checkString2);    
if(index!=-1)
    System.out.println("the first non repeating character in " +checkString2 
+" is at "+checkString2.charAt(index));
    else
    System.out.println("there is no repeating character in " +checkString2 
);

 index=f.getIndexOfFirstNonRepeatingChar(checkString3);    
if(index!=-1)
    System.out.println("the first non repeating character in " +checkString3 
+" is at "+checkString3.charAt(index));
else
System.out.println("there is no repeating character in " +checkString3 );

 index=f.getIndexOfFirstNonRepeatingChar(checkString4);    
if(index!=-1)
    System.out.println("the first non repeating character in " +checkString4 
+" is at "+checkString4.charAt(index));
    else
    System.out.println("there is no repeating character in " +checkString4 
);
    }
    
    public int getIndexOfFirstNonRepeatingChar(String checkString){
      char [] toCharArray = checkString.toCharArray();
if(toCharArray.length==1) 
    return 0; 
else if(toCharArray.length==2) {
    if(toCharArray[0]==toCharArray[1]) return -1;
    }else if(toCharArray.length==3){
        if (((checkString.charAt(0))!=(checkString.charAt(1))) &&  
(checkString.charAt(1) != checkString.charAt(2))){
            return 0;
        }else if((checkString.charAt(0)==checkString.charAt(1)) && 
(checkString.charAt(1)!=checkString.charAt(2))){
             return 2;
        }else if( (checkString.charAt(0)!=checkString.charAt(1)) && 
(checkString.charAt(1)==checkString.charAt(2)))
            return 0;
    } 


else{
     for(int i=0;i<toCharArray.length-2;i++){
        if (((checkString.charAt(i))!=(checkString.charAt(i+1))) &&  
(checkString.charAt(i+1) != checkString.charAt(i+2))){
              return i;
            }else if( (checkString.charAt(i) != checkString.charAt(i+1)) && 
(checkString.charAt(i+1)==checkString.charAt(i+2))){
                if(i-1<0){
                   return i;    
                }else{
 
                
                if( (checkString.charAt(i-1)==(checkString.charAt(i))) && 
(checkString.charAt(i+1)==checkString.charAt(i+2)))
                 continue;
                  else
                    return i;}
                    
            }else if( (checkString.charAt(i)== checkString.charAt(i+1)) && 
(checkString.charAt(i+1)!=checkString.charAt(i+2))){
                if( (i+3)<=checkString.length() ){ 
                    if(checkString.charAt(i+2)!=checkString.charAt(i+3)){
                        return i+2;
                    }else{
                        continue;
                        }
                    
                }else {return i+2;}}
        }
    }

     return -1;
    } 
}

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/algogeeks/-/AgtYjYsCg1UJ.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to