Social Icons

Friday, June 11, 2010

sameStarChar: Solutons for Javabat : 25


Question:

Returns true if for every '*' (star) in the string, if there are chars both immediately before and after the star, they are the same.

sameStarChar("xy*yzz") → true
sameStarChar("xy*zzz") → false
sameStarChar("*xa*az") → true

Source

Solution:

public boolean sameStarChar(String str) {
  int index=0;
  boolean b=false;
  if(str.length()<=1 || str.indexOf("*")==-1 || str.equals("**")) 
     b=true;
    
  for(int i=1; i<str.length()-1;i++){
    index=str.indexOf("*",i);
    if(index!=-1 && index!=str.length()-1 ){
      if( str.charAt(index-1)==str.charAt(index+1) )
        b= true;
      else
        b=false;
    }   
   
  }
  return b;
}
 
 
Blogger Templates http://slots.to/