We'll say that a lowercase 'g' in a string is happy if there is another 'g' immediately to its left or right. Return true if all the g's in the given string are happy.gHappy(xxggxx)  truegHappy(xxgxx)  falsegHappy(xxggyygxx)  falseThis is my code so far, which I think is very messy.  public boolean gHappy(String str)   {        for(int i=1;i<str.length()-1;i++)           {              if(str.charAt(i)=='g')                 if(str.charAt(i-1)!='g'&&str.charAt(i+1)!='g')                    return false;  }                     if(str.length()==1)      return false;  if(str.equals())      return true;if(str.charAt(str.length()-1)=='g')      if(str.charAt(str.length()-2)!='g')        return false;  return true;}
Checking that all occurrences of 'g' have another 'g' as a neighbor
java