Java > String-3 > gHappy (CodingBat Solution)

Problem:

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") → true
gHappy("xxgxx") → false
gHappy("xxggyygxx") → false


Solution:

public boolean gHappy(String str) {
  int len = str.length();
  boolean happy = true;

  for (int i = 0; i < len; i++) {
    if (str.charAt(i) == 'g') {
      if (i > 0 && str.charAt(i-1) == 'g')
        happy = true;
      else if (i < len-1 && str.charAt(i+1) == 'g')
        happy = true;
      else
        happy = false;
    }
  }
  return happy;
}


27 comments :

  1. This is shit. Better code (which works):

    public boolean gHappy(String str) {
    str = str.replaceAll("ggg", "");
    str = str.replaceAll("gg", "");
    return !str.contains("g");
    }

    ReplyDelete
    Replies
    1. That's a very smart and short way to solve this problem. Happy coding :)

      Delete
    2. great idea however, it wouldn't work for gggg. as the first replaceAll method would leave a remaining g. instead you could use regex "g{2,}" to find all occurrences of 2 or more g's.

      Delete
    3. Wrong, you must follow the rules of the exercise and use loops.

      Delete
    4. Wrong, it doesn't work for xxggggxx

      Delete
  2. Replies
    1. Yes! codingbat.com missing this use case.

      Delete
    2. If you comment out lines 8 and 10, it will work with "gagg". Alternatively, you could get rid of the 'happy' variable:

      public boolean gHappy(String str) {
      int len = str.length();
      for (int i = 0; i < len; i++) {
      if (str.charAt(i) == 'g'
      && !(i > 0 && str.charAt(i-1) == 'g')
      && !(i < len-1 && str.charAt(i+1) == 'g')) {
      return false;
      }
      }
      return true;
      }

      Delete
  3. public boolean gHappy(String str) {
    str="x"+str+"x";
    for(int i=1;i<str.length();i++){
    if(str.substring(i,i+1).equals("g")&&str.substring(i-1,i+2).indexOf("gg")==-1)
    return false;}
    return true;
    }

    ReplyDelete
  4. On line 12: happy = false; replace with return false;
    If you find first alone "g" than return false because like in an example "gagg" after first "g" happy will be false and than at the end "gg" will change happy to true

    ReplyDelete
  5. public boolean gHappy(String str) {

    String add="+"+str+"+";
    String check = "";

    for (int i=1; i<add.length(); i++)
    {
    if (add.charAt(i)!='g') check = check + "2";
    else if ((add.charAt(i)=='g' && add.charAt(i+1)=='g') ||
    (add.charAt(i)=='g' && add.charAt(i-1)=='g'))
    check = check + "1";
    else check = check + "0";
    }

    return (check.indexOf("0")<0);
    }

    ReplyDelete
  6. I don't understand anything.

    ReplyDelete
  7. public boolean gHappy(String str) {
    int l = str.length();
    str = "x" + str + "x";
    for (int i = 1; i<=l; i++) {
    if (str.charAt(i) == 'g' && str.charAt(i+1) != 'g' && str.charAt(i-1) != 'g') return false;
    }
    return true;
    }

    ReplyDelete
  8. boolean ishappy = true;
    int len = str.length();

    if (len == 1 && str.charAt(0) == 'g') return false;

    for (int i=str.length();i>0;i--){

    while ( i < len-1 && str.charAt(i) == 'g' && str.charAt(i+1) != 'g' && str.charAt(i-1) != 'g'
    || i < len-2 && str.charAt(len-1) == 'g' && str.charAt(len-2) != 'g'){
    return ishappy = false;
    }
    }

    return ishappy;

    ReplyDelete
  9. public boolean gHappy(String str) {
    if(str.length()==0) return true;
    if(str.length()==1) return false;
    if(str.charAt(0)=='g' && str.charAt(1)!='g') return false;
    if(str.charAt(str.length()-1)=='g' && str.charAt(str.length()-2)!='g') return false;

    for(int i=1;i<str.length()-1;i++){
    if((str.charAt(i)=='g' && str.charAt(i-1)!='g') && (str.charAt(i)=='g' && str.charAt(i+1)!='g')){
    return false;
    }
    }
    return true;
    }

    ReplyDelete
  10. return !str.matches("(.*)[^g]g[^g](.*)|g|(.*)[^g]g|g(^g)(.*)");

    ReplyDelete

  11. public static boolean gHappy(String str) {

    int len = str.length();

    for (int i = 0; i < len; i++) {

    if (str.charAt(i) == 'g') {
    if (i > 0 && str.charAt(i - 1) == 'g')
    continue;
    else if (i < len - 1 && str.charAt(i + 1) == 'g')
    continue;
    else
    return false;
    }
    }
    return true;
    }

    this works find.
    you miss g which there is another gg after that

    ReplyDelete
  12. public boolean gHappy(String str) {
    if (str.length() == 1)
    {
    return false;
    }
    for (int i = 0; i < str.length(); i++)
    {
    int count = 0;
    while (i < str.length() && str.charAt(i) == 'g')
    {
    count++;
    i++;
    }
    if (count == 1)
    {
    return false;
    }
    }
    return true;
    }

    ReplyDelete
  13. public boolean gHappy(String str) {
    return !str.replaceAll("ggg", "").replaceAll("gg", "").contains("g");
    }

    ReplyDelete
  14. THIS CONDITION WORKS FOR "gggg", "ggggg" (for any num of continuous g's)

    public boolean gHappy(String str)
    {
    str = str.replace("gg", "++");
    str = str.replace("+g", "++");
    str = str.replace("g", "-");
    int count = 0;

    for(int i = 0; i 0)
    {
    return false;
    }
    return true;
    }

    ReplyDelete
    Replies
    1. CORRECT CODE:

      public boolean gHappy(String str)
      {
      str = str.replace("gg", "++");
      str = str.replace("+g", "++");
      str = str.replace("g", "-");
      int count = 0;

      for(int i = 0; i 0)
      {
      return false;
      }
      return true;
      }

      Delete
  15. public boolean gHappy(String str) {
    String str1=str.replaceAll("gg","1");
    String str2=str1.replaceAll("1g","1");
    String str3=str2.replaceAll("g1","1");
    boolean happy=true;
    int n=str3.length();
    for(int i=0;i<n;i++){
    if(str3.charAt(i)=='g'){
    happy=false;
    break;
    }
    }

    return happy;
    }

    ReplyDelete
  16. public boolean gHappy(String str) {
    int countOne = 0;
    int countTwo = 0;
    for (int i = str.length()-1; i>=0; i--) {
    if (str.charAt(i) == 'g') countTwo++;
    if (i > 0 && str.charAt(i) == 'g' && str.charAt(i - 1) == 'g') countOne++;
    if (countTwo - 2 * countOne > 0) return false;
    }
    return true;
    }

    ReplyDelete
  17. public boolean gHappy(String str) {
    boolean a=true;
    int t=str.length();
    for(int i=0;i0 &&str.charAt(i-1)=='g')
    a=true;
    else if(i<t-1 &&str.charAt(i+1)=='g')
    a=true;
    else a=false;
    }
    }return a;
    }

    ReplyDelete
  18. There are much better ones on here, a couple I do not understand at all in fact. But what I came up with shows logic as it happens.

    public boolean gHappy(String str) {
    boolean result = true;
    for (int i = 0; i < str.length(); ++i) {
    boolean isG = (str.charAt(i) == 'g');
    boolean preG = (i != 0 && str.charAt(i-1) == 'g');
    boolean postG = (i != str.length()-1 && str.charAt(i+1) == 'g');
    if (isG && (!preG && !postG)) result = false;
    }
    return result;
    }

    ReplyDelete
  19. public boolean gHappy(String str) {
    if(str.length() == 0)
    return true;

    boolean flag = false;
    for(int i = 1; i < str.length(); i++){
    if(str.charAt(i) == 'g'){
    if(str.charAt(i-1) == 'g'){
    flag = true;
    i++;
    }
    else
    flag = false;
    }
    }
    return flag;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact